如何在QT中更新窗口? [英] How to update a window in QT?

查看:97
本文介绍了如何在QT中更新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在QtCreator中进行一个简单的项目,在这里您将文本输入到line_edit中,然后在单击按钮后将其打印出来.它可以工作,但是我需要调整窗口大小才能查看更新/更改的显示.

Im working on a simple project in QtCreator where you input text into a line_edit which then gets printed after clicking a button. It works but I need to resize the window in order to see the updated/changed display.

因此从main.cpp开始,经过一些测试,我将其保留为默认设置:

So starting off with the main.cpp, I have left it as default after some tests:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

那是我在上面谈论的问题.我决定添加 w.update(); ,看看是否能解决问题,但没有解决.我以为是因为程序没有循环,所以我在while(true)循环中输入了代码,但也无济于事.

That has the issue I was talking about above. I decided to add 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.

mainwindow.cpp文件如下:

The mainwindow.cpp file is as follows:

#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

QT中是否有QObject或预定义的函数可以让我更新窗口或在检测到用户更改后自动更新窗口?

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?

UI文件也可能很重要:

The UI file might be of importance as well:

<?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>

推荐答案

问题不是GUI的更新,而是 QLabel 不会更改大小,初始大小取决于初始文本,如果您设置的文本较大,则仅显示部分文本.要将标签的大小调整为文本的大小,必须使用 adjustSize() :

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. To adjust the size of the label to the size of the text you must use adjustSize():

void MainWindow::setText()
{
    QString temp = ui->inputText->text();
    ui->displayLabel->setText(temp);
    ui->displayLabel->adjustSize();
}

另一方面,在Qt5中,建议使用新的连接语法,因为它们具有一些优点,如所示.文档,您必须将代码更改为:

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:

connect(ui->textBtn, &QPushButton::clicked, this, &MainWindow::setText);

这篇关于如何在QT中更新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆