I'm totally new to QT. I've written a little form with 4 radio buttons, click button and plain text edit field (in QT Creator). My goal is to press push button and get a text change in the plaintextedit ("Some text here"). Unfortunately I can't find the right formula for connecting the button and the text field, so I get an error, I can't figure out yet. Here's the code (I omit the GUI XML part):
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");
}
Here's the error output:
/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*'
If I write just
QObject::connect(pushButton,SIGNAL(clicked()),SLOT(on_pushButton_clicked()));
then the error output slightly changes, but the essence is still the same. How can I solve this problem?
--EDIT--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");
}
As a result, I get SIGSEGV with the following output:
amember "1on_pushButton_clicked()" char *
asender @0x2 QObject
asignal "2clicked()" char *
atype Qt::AutoConnection(0) Qt::ConnectionType
this @0x7fffffffe640 QObject
It also points to the following lines in 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);
You get this error because connect
use pointers. You should use pointers:
QRadioButton *radioButton,*radioButton_2,*radioButton_3,*radioButton_4;
QPushButton *pushButton;
QPlainTextEdit *plainTextEdit;
When you use pointer every .
replace with ->
. Compiler tells you about mistake with next error:
...(maybe you meant to use '->' ?) radioButton_2.show();
And connect should be:
connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
or
connect(pushButton,SIGNAL(clicked()),SLOT(on_pushButton_clicked()));
On future: you can also write this:
QObject obj1;
QObject obj2;
connect(&obj1,SIGNAL(someSigna()),&obj2,SLOT(slot()));
As you can see, you need &
You need to write QObject::connect
in not QObject
subclasses (for example in main.cpp
) In your MainWindow
you can write just connect
Also when you use pointers you should allocate memory with new
. For example:
pushButton = new QPushButton(this);//in constructor
//and so on
where this
shows us that this
(our MainWindow
) is parent, so pushButton
will appear in MainWindow
, not in the separate window.
I think I mentioned all big mistakes in this code.
on_pushButton_clicked()
is slot of mainwindow, not a plaintexteditTry next:
pushButton = new QPushButton(this);
plainTextEdit = new QPlainTextEdit(this);
connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));