yessirsir
11th June 2016, 21:52
Hello! I am just starting up with qt and c++
I have a small project to practice
I would like the QPushButton to append this string -> "text" inside the text box after I click it.
Clicking twice would result in having "texttext" and so on.
11976
I have these files so far:
test.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.h :
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void addTextToLabel();
private:
Ui::MainWindow *ui;
};
#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();
}
and mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(this- >addTextToLabel()));
}
void MainWindow::addTextToLabel()
{
ui->textEdit->setText(ui->textEdit->toPlainText() + "test");
}
MainWindow::~MainWindow()
{
delete ui;
}
but I am getting this error:
QObject::connect: No such slot MainWindow::this->addTextToLabel() in ..\test\mainwindow.cpp:10
QObject::connect: (sender name: 'pushButton')
QObject::connect: (receiver name: 'MainWindow')
I am not sure what I am doing wrong (very beginner)
Any one could help me out ?
Added after 18 minutes:
Someone helped figure it out :)
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel()));
I have a small project to practice
I would like the QPushButton to append this string -> "text" inside the text box after I click it.
Clicking twice would result in having "texttext" and so on.
11976
I have these files so far:
test.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.h :
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void addTextToLabel();
private:
Ui::MainWindow *ui;
};
#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();
}
and mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(this- >addTextToLabel()));
}
void MainWindow::addTextToLabel()
{
ui->textEdit->setText(ui->textEdit->toPlainText() + "test");
}
MainWindow::~MainWindow()
{
delete ui;
}
but I am getting this error:
QObject::connect: No such slot MainWindow::this->addTextToLabel() in ..\test\mainwindow.cpp:10
QObject::connect: (sender name: 'pushButton')
QObject::connect: (receiver name: 'MainWindow')
I am not sure what I am doing wrong (very beginner)
Any one could help me out ?
Added after 18 minutes:
Someone helped figure it out :)
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel()));