我对QT完全不熟悉。我用4个单选按钮,单击按钮和纯文本编辑字段(在QT Creator中)编写了一个小表单。我的目标是按下按钮并在plaintextedit中进行文本更改("有些文字在这里")。不幸的是,我无法找到连接按钮和文本字段的正确公式,因此我收到错误,我还无法弄清楚。这是代码(我省略了GUI XML部分):
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()));
然后错误输出略有变化,但本质仍然是相同的。我怎么解决这个问题?
- 编辑 - 14年11月15日 -
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 :(得分: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::connect
子类中撰写QObject
(例如在main.cpp
中)在MainWindow
中,您只需撰写connect
此外,当您使用指针时,您应该使用new
分配内存。例如:
pushButton = new QPushButton(this);//in constructor
//and so on
其中this
向我们显示this
(我们的MainWindow
)是父级,因此pushButton
将显示在MainWindow
中,而不会显示在单独的窗口中。< / p>
我想我在这段代码中提到了所有重大错误。
on_pushButton_clicked()
是主窗口的插槽,而不是plaintextedit,请尝试下一步:
pushButton = new QPushButton(this);
plainTextEdit = new QPlainTextEdit(this);
connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));