代码之家  ›  专栏  ›  技术社区  ›  NESHOM

根据Qt中的显示/屏幕大小定位动态QDialog

  •  0
  • NESHOM  · 技术社区  · 8 年前

    我想定位并显示一个关于显示器的对话框。该对话框有一个标签。该对话框将根据标签的大小展开。

    下面是我不工作的代码。它尝试在屏幕中央显示对话框(默认情况下,对话框显示在中央,此代码仅用于评估):

    QDialog splash;
    QVBoxLayout *laySplash = new QVBoxLayout(&splash);
    QLabel *lblText = new QLabel;
    laySplash->addWidget(lblText);
    lblText->setText("hello world! hello world! hello world! hello world! hello world! ");
    
    int intScreenHeight = QApplication::desktop()->screenGeometry().height();
    int intScreenWidth = QApplication::desktop()->screenGeometry().width();
    
    splash.layout()->update();
    splash.layout()->activate();
    
    int intSplashLeft = (intScreenWidth / 2) - (splash.width() / 2);
    int intSplashTop = (intScreenHeight /2) - (splash.height() / 2);
    splash.move(intSplashLeft, intSplashTop);
    
    splash.exec();
    

    我做错了什么?

    (我在Linux中使用Qt5)

    2 回复  |  直到 8 年前
        1
  •  1
  •   eyllanesc    8 年前

    您案例中的问题是,布局只会在小部件可见后更新其大小,一种可能的解决方案是通过调用该方法强制它 adjustSize() QDialog .

    有几种方法可以使小部件居中:

    1. 第一种形式:

    QDialog splash;
    QVBoxLayout *laySplash = new QVBoxLayout(&splash);
    QLabel *lblText = new QLabel;
    laySplash->addWidget(lblText);
    lblText->setText("hello world! hello world! hello world! hello world! hello world! ");
    splash.adjustSize();
    
    splash.setGeometry(
        QStyle::alignedRect(
            Qt::LeftToRight,
            Qt::AlignCenter,
            splash.size(),
            qApp->desktop()->availableGeometry()
        )
    );
    
    splash.exec();
    
    1. 第二种形式:

    QDialog splash;
    QVBoxLayout *laySplash = new QVBoxLayout(&splash);
    QLabel *lblText = new QLabel;
    laySplash->addWidget(lblText);
    lblText->setText("hello world! hello world! hello world! hello world! hello world! ");
    splash.adjustSize();
    QPoint p = QApplication::desktop()->geometry().center()-splash.rect().center();
    splash.move(p);
    
    splash.exec();
    
    1. 第三种形式(您的方法):

    QDialog splash;
    QVBoxLayout *laySplash = new QVBoxLayout(&splash);
    QLabel *lblText = new QLabel;
    laySplash->addWidget(lblText);
    lblText->setText("hello world! hello world! hello world! hello world! hello world! ");
    
    splash.adjustSize();
    int intScreenHeight = QApplication::desktop()->screenGeometry().height();
    int intScreenWidth = QApplication::desktop()->screenGeometry().width();
    
    int intSplashLeft = (intScreenWidth / 2) - (splash.width() / 2);
    int intSplashTop = (intScreenHeight /2) - (splash.height() / 2);
    splash.move(intSplashLeft, intSplashTop);
    
    splash.exec();
    
        2
  •  1
  •   Gurushant GeneCode    8 年前

    试试我的代码 创建一个名为set geometry的函数,并传递QDialog指针和相对于屏幕的大小百分比。如果希望对话框的大小为总屏幕的50%,则通过50

    void setGeometry(QDialog *dialogBox, int PercentageOfScreen)
    {
        int x, y, w, h;
        QRect rect = QApplication::desktop()->screenGeometry();
        int screen_width = rect.width();
        int screen_height = rect.height();
    
        //Represent Percentage in decimals
        float PercentageOfScreenFloat = (float)PercentageOfScreen/100;
    
        //Calculate w and h
        w = (PercentageOfScreenFloat * screen_width);
        h = (PercentageOfScreenFloat * screen_height);
    
        //Check for max and min size hints
        int minW = dialogBox->minimumWidth();
        int minH = dialogBox->minimumHeight();
    
        int maxW = dialogBox->maximumWidth();
        int maxH = dialogBox->maximumHeight();
    
        if(w<minW || h<minH)
        {
            w = minW;
            h = minH;
        }
        else if(w>maxW || h>maxH)
        {
            w = maxW;
            h = maxH;
        }
    
        //Now Calculate x and y
        x = screen_width / 2;
        x = x - w / 2;
    
        y = screen_height / 2;
        y = y - h / 2;
    
        dialogBox->setGeometry(x, y, w, h);
    
    }