Home > OS >  I started making a qt6 widget application. How do I get it to start in the upper right corner of the
I started making a qt6 widget application. How do I get it to start in the upper right corner of the

Time:01-04

⁸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'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 ? 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;

CodePudding user response:

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

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

CodePudding user response:

Try this:

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