在 Qt 中根据 display/screen 大小定位动态 QDialog
Position a dynamic QDialog with respect to display/screen size in Qt
我想定位然后显示一个关于显示器的对话框。该对话框有一个标签。对话框根据标签的大小展开。
下面是我的代码不起作用。它试图在屏幕中央显示对话框(对话框默认显示在中央,此代码仅用于评估):
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)
你的问题是布局只会在小部件可见后更新它的大小,一个可能的解决方案是通过调用 QDialog
的方法 adjustSize()
来强制它.
有几种方法可以使小部件居中:
- 第一种形式:
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();
- 第二形式:
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();
- 第三种形式(你的方法):
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();
试试我的代码,它有效
创建一个名为 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);
}
我想定位然后显示一个关于显示器的对话框。该对话框有一个标签。对话框根据标签的大小展开。
下面是我的代码不起作用。它试图在屏幕中央显示对话框(对话框默认显示在中央,此代码仅用于评估):
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)
你的问题是布局只会在小部件可见后更新它的大小,一个可能的解决方案是通过调用 QDialog
的方法 adjustSize()
来强制它.
有几种方法可以使小部件居中:
- 第一种形式:
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();
- 第二形式:
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();
- 第三种形式(你的方法):
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();
试试我的代码,它有效 创建一个名为 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);
}