Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 单击对话框中心屏幕或中心对话框_C++_Qt - Fatal编程技术网

C++ 单击对话框中心屏幕或中心对话框

C++ 单击对话框中心屏幕或中心对话框,c++,qt,C++,Qt,我有以下代码: QString text = QInputDialog::getText(this, tr("Key:"), tr("Key:"), QLineEdit::Password, "", &ok); 我无法使此对话框显示在中央屏幕上,我查看了WindowFlags,并尝试将QInputDialog初始化为变量,调用move,然后调用getText,确保它也不起作用,因为窗口尚未初始化并将被移动。我不想在调用getText后创建另一个线程/计时器来移动它,这不是一个好主意 有

我有以下代码:

QString text = QInputDialog::getText(this, tr("Key:"), tr("Key:"), QLineEdit::Password, "", &ok);
我无法使此对话框显示在中央屏幕上,我查看了WindowFlags,并尝试将QInputDialog初始化为变量,调用move,然后调用getText,确保它也不起作用,因为窗口尚未初始化并将被移动。我不想在调用getText后创建另一个线程/计时器来移动它,这不是一个好主意


有什么想法吗?

它将不起作用,因为当您使用QInputDialog作为变量时,就不应该再使用静态方法了。所以您需要类似于下一个代码的代码,它相当于静态方法,因为默认情况下调用exec modal对话框。对我来说很正常:

QString mText;
QInputDialog *inp = new QInputDialog(this);
inp->setLabelText(tr("Key:"));
inp->setWindowTitle(tr("Key:"));
inp->setTextEchoMode(QLineEdit::Password);
inp->adjustSize();
inp->move(QApplication::desktop()->screen()->rect().center() - inp->rect().center());
if(inp->exec() == QDialog::Accepted)
    mText = inp->textValue();
qDebug() << mText;

再次感谢您!这正是我想要的
QSize screenSize = QApplication::desktop()->geometry().size();
int primaryScreenWidth = screenSize.width();
int primaryScreeHeight= screenSize.height();
int widgetWidth = inp->width();
int widgetHeight= inp->height();
inp->move(primaryScreenWidth/2 - widgetWidth/2, primaryScreeHeight/2 - widgetHeight/2);