我對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);
QRadioButton * RADIOBUTTON,* radioButton_2,* radioButton_3,* radioButton_4; QPushButton * pushButton; QPlainTextEdit * plainTextEdit; ,然後用新的 – 2014-11-05 06:50:25