在运行时尝试更改QPushButton文本时,我做错了什么

What am I doing wrong trying to change QPushButton text at runtime?

本文关键字:什么 错了 文本 QPushButton 运行时      更新时间:2024-09-21

我对使用QTCreator和Linux图形开发进行编码是个新手。我在这里做错了什么,可能是微不足道的事?

我可以在不使用form方法的情况下编写这些代码并使其轻松工作,但我真的更喜欢使用图形界面,因为它应该更快(一旦我知道编译器希望我做什么(。

当我输入这个代码时,它告诉我btnStopGo没有在这个上下文中定义。我使用表单界面将按钮放到表单上。

代码在文件mainwindow.cpp 中

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
...
void MainWindow::on_btnStopGo_clicked()
{
if (bDoStream){
if (bStreaming){
//TODO: Send stop stream signal, save stream to file, dispose stream
bStreaming = false;
btnStopGo->setText("Go");
}
else{
btnStopGo->setText("Stop");
bStreaming = true;
// TODO: request a stream of quotes
}
}
else {
// TODO: Get a single quote tick
}
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
...
private slots:
...
void on_btnSelect_clicked();
void on_btnStopGo_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

项目中的其他可见文件有:Hello2.pro、main.cpp和mainwindow.ui.main.cpp声明mywindow为访问主窗口的变量。我试过用";ui.btnStopGo";,mywindow.btnStopGo,用户界面->btnStopGo(炸毁编译器(,以及我能想到的所有其他组合。到目前为止,它们都不起作用。

我不知道这是否相关,但我正在使用:Qt创建者4.11.0基于Qt 5.12.8(GCC 9.3.0,64位(在Linux Mint 20 Cinnamon上手动添加KDE。

如果将按钮放在表单设计器中,并调用按钮btnStopGo,则它最终会编译为ui_mainwindow.h.cc文件。您自己编写的MainWindow类没有名为btnStopGo的(成员(变量。它有一个变量ui,指向表单设计器生成的用户界面。

请改用ui->btnStopGo->setText("stop")