2014-11-04 137 views
1

我對QT完全陌生。我寫了一個帶有4個單選按鈕的小表單,單擊按鈕和純文本編輯字段(在QT Creator中)。我的目標是按下按鈕並在plaintextedit中獲得文本更改(「此處顯示一些文本」)。不幸的是,我找不到連接按鈕和文本字段的正確公式,所以我得到一個錯誤,我還找不到。下面的代碼(我省略了GUI XML部分):QT按鈕click to change plaintextedit

mainwindow.h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QRadioButton> 
#include <QPushButton> 
#include <QPlainTextEdit> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

public slots: 
    void on_pushButton_clicked(); 

private: 
    Ui::MainWindow *ui; 
    QRadioButton radioButton,radioButton_2,radioButton_3,radioButton_4; 
    QPushButton pushButton; 
    QPlainTextEdit plainTextEdit; 
}; 

#endif // MAINWINDOW_H 

main.cpp中:

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

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

    return a.exec(); 
} 

mainwindow.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
//#include <QPushButton> 
//#include <QPlainTextEdit> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    QObject::connect(pushButton,SIGNAL(clicked()),plainTextEdit,SLOT(on_pushButton_clicked())); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::on_pushButton_clicked() 
{ 
    plainTextEdit.setPlainText("Some text here"); 
} 

這裏的錯誤輸出:

/home/user/workspace/ButtonRadio/mainwindow.cpp:-1: In constructor 'MainWindow::MainWindow(QWidget*)': 
/home/user/workspace/ButtonRadio/mainwindow.cpp:11: error: no matching function for call to 'MainWindow::connect(QPushButton&, const char*, QPlainTextEdit&, const char*)' 
/home/user/workspace/ButtonRadio/mainwindow.cpp:11: candidates are: 
/usr/include/qt4/QtCore/qobject.h:204: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType) 
/usr/include/qt4/QtCore/qobject.h:204: note: no known conversion for argument 1 from 'QPushButton' to 'const QObject*' 
/usr/include/qt4/QtCore/qobject.h:217: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType) 
/usr/include/qt4/QtCore/qobject.h:217: note: no known conversion for argument 1 from 'QPushButton' to 'const QObject*' 
/usr/include/qt4/QtCore/qobject.h:337: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const 
/usr/include/qt4/QtCore/qobject.h:337: note: no known conversion for argument 1 from 'QPushButton' to 'const QObject*' 

如果我寫的只是

QObject::connect(pushButton,SIGNAL(clicked()),SLOT(on_pushButton_clicked())); 

則錯誤輸出稍有變化,但本質上仍然是相同的。我怎麼解決這個問題?

- 編輯 - 15.11.14--

mainwindow.h: ...

private: 
    Ui::MainWindow *ui; 
    QRadioButton *radioButton,*radioButton_2,*radioButton_3,*radioButton_4; 
    QPushButton *pushButton; 
    QPlainTextEdit *plainTextEdit; 
}; 

mainwindow.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow), 
    pushButton(new QPushButton(this)), 
    plainTextEdit(new QPlainTextEdit(this)) 
{ 
    ui->setupUi(this); 
    //pushButton = new QPushButton(this); 
    //plainTextEdit = new QPlainTextEdit(this); 
    QObject::connect(pushButton,SIGNAL(clicked()),plainTextEdit,SLOT(on_pushButton_clicked())); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::on_pushButton_clicked() 
{ 
    plainTextEdit->setPlainText("Some text here"); 
} 

其結果是,我得到具有以下輸出的SIGSEGV:

amember "1on_pushButton_clicked()" char * 
asender @0x2 QObject 
asignal "2clicked()" char * 
atype Qt::AutoConnection(0) Qt::ConnectionType 
this @0x7fffffffe640 QObject 

它還指出在qobject.h下面幾行:

339 { return connect(asender, asignal, this, amember, atype); } 
349 T qFindChild(const QObject *o, const QString &name = QString()); 
350 QList<T> qFindChildren(const QObject *oobj, const QString &name = QString()); 
351 QList<T> qFindChildren(const QObject *o, const QRegExp &re); 
+0

QRadioButton * RADIOBUTTON,* radioButton_2,* radioButton_3,* radioButton_4; QPushButton * pushButton; QPlainTextEdit * plainTextEdit; ,然後用新的 – 2014-11-05 06:50:25

回答

0

你得到這個錯誤,因爲connect使用指針。你應該使用指針:

QRadioButton *radioButton,*radioButton_2,*radioButton_3,*radioButton_4; 
QPushButton *pushButton; 
QPlainTextEdit *plainTextEdit; 

當使用指針每.->取代。編譯器告訴你有一個錯誤的錯誤:

...(maybe you meant to use '->' ?) radioButton_2.show(); 

而且連接應該是:

connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked())); 

connect(pushButton,SIGNAL(clicked()),SLOT(on_pushButton_clicked())); 

在未來:你也可以這樣寫:

QObject obj1; 
QObject obj2; 
connect(&obj1,SIGNAL(someSigna()),&obj2,SLOT(slot())); 

正如你所看到的,你需要&

你需要在沒有QObject子寫QObject::connect(例如在main.cpp)在你MainWindow當您使用指針,你應該new分配內存,你可以寫只是connect

也。例如:

pushButton = new QPushButton(this);//in constructor 
//and so on 

其中this告訴我們,this(我們MainWindow)爲母公司,所以pushButton將出現在MainWindow,而不是在單獨的窗口。

我想我提到了這段代碼中的所有重大錯誤。

on_pushButton_clicked()是主窗口的槽,而不是下一個plaintexteditTry:

pushButton = new QPushButton(this); 
plainTextEdit = new QPlainTextEdit(this); 
connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked())); 
+0

'* radioButton_2','* radioButton_3','* radioButton_4'分配內存。 – coyotte508 2014-11-04 19:24:09

+0

謝謝!抱歉,我的評論漫長的延遲。 我已經完成了你所說的 - 將變量改爲指針(你是對的!),爲pushButton創建了一個分配,改爲「。」到on_pushButton_clicked()中的「 - >」,但最後我得到了SIGSEGV Segmentation Fault。無法弄清楚如何在此回覆窗口中發佈代碼... – user1274925 2014-11-14 20:30:30

+0

@ user1274925您是否也爲明文分配了內存?如果不是,那麼嘗試去做。如果它不起作用,請按編輯您的答案,並添加您當前的代碼。 – Chernobyl 2014-11-14 20:38:13