我是使用 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
Other visible files in the project are : Hello2.pro, main.cpp, and mainwindow.ui. main.cpp declares mywindow as the variable to access the main window. I've tried using "ui.btnStopGo", mywindow.btnStopGo, ui->btnStopGo (blows up the compiler), and every other combo I can think of. None of them work so far.
I don't know if this is relevant but I'm using: Qt Creator 4.11.0 Based on Qt 5.12.8 (GCC 9.3.0, 64 bit) on a Linux Mint 20 Cinnamon with KDE added manually.
uj5u.com熱心網友回復:
如果您將按鈕放在表單設計器中,并呼叫 button btnStopGo,那么它最終會編譯到ui_mainwindow.hand.cc檔案中。您MainWindow自己撰寫的類沒有名為btnStopGo. 它確實有一個變數ui,它指向從表單設計器生成的用戶界面。
改為使用ui->btnStopGo->setText("stop")。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418533.html
標籤:
