[問題] QT關於slot的程式沒反應

看板C_and_CPP作者 (iter)時間14年前 (2011/07/28 14:53), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) vs2010 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) QT4 問題(Question): 原po是想練習一下qt用法 本來想弄一個小視窗 裡面塞一個按鈕(PushButton)和一個文字條(LineEdit) 希望可以按下按鈕然後可以在文字格上面印出自訂的文字 不知道哪裡寫錯signal觸發後slot的副程式無法被啟動 程式碼(Code): #include <QtGui/QMainWindow> #include "ui_interface_6.h" #include <QLineEdit> #include <QPushButton> class Interface_6 : public QMainWindow #include "interface_6.h" #include <QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Interface_6 w; return a.exec(); } { Q_OBJECT public : //我的constructor Interface_6(QWidget *parent = 0, Qt::WFlags flags = 0); ~Interface_6(); public slots: //slot的function void change(); private: Ui::Interface_6Class ui; //我的按紐和文字條 QLineEdit* line; QPushButton* btn; }; ///////// #include "interface_6.h" #include <qpushbutton> #include <qlineedit.h> #include <qwidget.h> #include <qhboxlayout> #include <Qstring> Interface_6::Interface_6(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { QHBoxLayout* layout = new QHBoxLayout; btn = new QPushButton; line = new QLineEdit; QObject::connect( btn,SIGNAL(click()),line,SLOT(change()) ); layout->addWidget(btn); layout->addWidget(line); QWidget* box = new QWidget; box->setLayout(layout); box->show(); ui.setupUi(this); } Interface_6::~Interface_6() { } void Interface_6::change() { line->setText("yes"); } //////////// #include "interface_6.h" #include <QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Interface_6 w; return a.exec(); } 預期的正確結果(Expected Output): 按鈕按下去之後希望可以在旁邊顯示文字 錯誤結果(Wrong Output): 按鈕按了沒反應 補充說明(Supplement): 希望有大大願意撥空回答我的Q_Q -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.68.170

07/28 15:53, , 1F
connect( btn,SIGNAL(click()),line,SLOT(change()) );
07/28 15:53, 1F

07/28 15:54, , 2F
line改this 你收訊號的是this這個物件,不是line這個
07/28 15:54, 2F

07/28 15:58, , 3F
ya大大真是太感謝了!!!! 終於會動了^^"
07/28 15:58, 3F
文章代碼(AID): #1ECGTOsb (C_and_CPP)