PDA

View Full Version : Beginner error I'd need help with



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()));

^NyAw^
13th June 2016, 15:52
Hi,

This


connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(this- >addTextToLabel()));


should be


connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel()));


Please read how SIGNALS and SLOTS work first.

anda_skoa
13th June 2016, 16:20
Please read how SIGNALS and SLOTS work first.
The thread starter had actually already found out how it worked, see the edit :)

Cheers,
_

^NyAw^
13th June 2016, 17:16
Hi,

Ups! I have not noticed! :rolleyes: