[英]How to update a window in QT?
Im working on a simple project in QtCreator where you input text into a line_edit which then gets printed after clicking a button. 我正在QtCreator中进行一个简单的项目,在这里您将文本输入到line_edit中,然后单击按钮后将其打印出来。 It works but I need to resize the window in order to see the updated/changed display. 它可以工作,但是我需要调整窗口大小以查看更新/更改的显示。
So starting off with the main.cpp, I have left it as default after some tests: 因此,从main.cpp开始,经过一些测试,我将其保留为默认设置:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
That has the issue I was talking about above. 那就是我上面所说的问题。 I decided to add w.update();
我决定添加w.update();
and see if that fixed the issue, it did not. 看看是否能解决问题,但没有解决。 I thought maybe it was because the program was not looping, so I entered the code in a while(true) loop which also was to no avail. 我以为是因为程序没有循环,所以我在while(true)循环中输入了代码,但也无济于事。
The mainwindow.cpp file is as follows: mainwindow.cpp文件如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->textBtn, SIGNAL(clicked(bool)), this, SLOT(setText()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setText()
{
QString temp = ui->inputText->text();
ui->displayLabel->setText(temp);
}
MainWindow.hpp: MainWindow.hpp:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void setText();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Is there a QObject or predifined function in QT that allows me to update the window or automatically updates the window after a detected user change? QT中是否有QObject或预定义的函数可以让我更新窗口或在检测到用户更改后自动更新窗口?
Edit: The UI file might be of importance as well: 编辑:UI文件可能也很重要:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>554</width>
<height>463</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLabel" name="displayLabel">
<property name="geometry">
<rect>
<x>140</x>
<y>150</y>
<width>251</width>
<height>91</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>130</x>
<y>30</y>
<width>251</width>
<height>81</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="inputText"/>
</item>
<item>
<widget class="QPushButton" name="textBtn">
<property name="text">
<string>Display Text</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>554</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
The problem is not the update of the GUI but the QLabel
does not change size, the initial size depends on the initial text, and if you set a text with larger size only part of the text will be displayed. 问题不在于GUI的更新,而是QLabel
不会更改大小,初始大小取决于初始文本,如果将文本设置为较大的大小,则仅显示部分文本。 To adjust the size of the label to the size of the text you must use adjustSize()
: 要将标签的大小调整为文本的大小,必须使用adjustSize()
:
void MainWindow::setText()
{
QString temp = ui->inputText->text();
ui->displayLabel->setText(temp);
ui->displayLabel->adjustSize();
}
On the other hand in Qt5 it is advisable to use the new connection syntax since they have several advantages as indicated by the docs , in your case you must change your code to: 另一方面,在Qt5中,建议使用新的连接语法,因为它们具有docs所指出的若干优点,在这种情况下,您必须将代码更改为:
connect(ui->textBtn, &QPushButton::clicked, this, &MainWindow::setText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.