简体   繁体   English

我开始制作一个 qt6 小部件应用程序。 当我运行应用程序时,如何让它在屏幕的右上角启动

[英]I started making a qt6 widget application. How do I get it to start in the upper right corner of the screen when I run the application

⁸I started making a qt6 widget application. ⁸我开始制作一个 qt6 小部件应用程序。 How do I get it to start in the upper right corner of the screen when I run the application.I've been struggling for hours but couldn't find the appropriate code to do this with cmake.Is there a way to install the cmake QDesktopWidget library?当我运行应用程序时,如何让它在屏幕的右上角启动。我已经苦苦挣扎了几个小时,但找不到合适的代码来使用 cmake 执行此操作。有没有办法安装 cmake QDesktopWidget 库? I hoped that the code in the bold text would start in the upper right corner, but it was not as I expected, I can't find the problem.我希望加粗文字里的代码是从右上角开始的,结果不是我想的那样,我找不到问题所在。

#include "widget.h"
#include "./ui_widget.h"
#include <QScreen>
#include <QRect>
#include <QSize>
#include <QApplication>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::WindowType::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setStyleSheet(".QFrame{background-color: red; border: 1px solid black; border-radius: 10px;}");
       QRect sc = QGuiApplication::primaryScreen()->geometry();
       QWidget::move(sc.top( ),sc.right());
       Widget::show();

}

Widget::~Widget()
{
    delete ui;

Assuming that the window is the size of the screen, swapping sc.top() and sc.right() and subtracting the width of the widget from sc.right() should work假设 window 是屏幕的大小,交换 sc.top() 和 sc.right() 并从 sc.right() 中减去小部件的宽度应该可行

This plus removing either the call to setWindowFlags or to setAttribute will display the widget in the top right corner Alternatively you can add something to display in the widget, and that would show up, its just that the combination makes the widget itself invisible这加上删除对 setWindowFlags 或 setAttribute 的调用将在右上角显示小部件或者您可以添加要在小部件中显示的内容,并且会显示出来,只是组合使小部件本身不可见

Try this:试试这个:

this->move(QApplication::primaryScreen()->geometry().right()- this->rect().right(),
           QApplication::primaryScreen()->geometry().top()- this->rect().top());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM