Results 1 to 4 of 4

Thread: Beginner error I'd need help with

  1. #1
    Join Date
    Jun 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Beginner error I'd need help with

    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.

    2016-06-11 19_13_39-mainwindow.ui - test - Qt Creator.jpg


    I have these files so far:

    test.pro:

    Qt Code:
    1. QT += core gui
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. TARGET = test
    4. TEMPLATE = app
    5. SOURCES += main.cpp\
    6. mainwindow.cpp
    7. HEADERS += mainwindow.h
    8. FORMS += mainwindow.ui
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h :

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. private slots:
    10. void addTextToLabel();
    11.  
    12. private:
    13. Ui::MainWindow *ui;
    14. };
    15.  
    16. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    and mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(this- >addTextToLabel()));
    10. }
    11.  
    12. void MainWindow::addTextToLabel()
    13. {
    14. ui->textEdit->setText(ui->textEdit->toPlainText() + "test");
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    To copy to clipboard, switch view to plain text mode 

    but I am getting this error:

    Qt Code:
    1. QObject::connect: No such slot MainWindow::this->addTextToLabel() in ..\test\mainwindow.cpp:10
    2. QObject::connect: (sender name: 'pushButton')
    3. QObject::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 

    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()));
    Last edited by yessirsir; 11th June 2016 at 21:52.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Beginner error I'd need help with

    Hi,

    This
    Qt Code:
    1. connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(this- >addTextToLabel()));
    To copy to clipboard, switch view to plain text mode 

    should be
    Qt Code:
    1. connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel()));
    To copy to clipboard, switch view to plain text mode 

    Please read how SIGNALS and SLOTS work first.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Beginner error I'd need help with

    Quote Originally Posted by ^NyAw^ View Post
    Please read how SIGNALS and SLOTS work first.
    The thread starter had actually already found out how it worked, see the edit

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    ^NyAw^ (13th June 2016)

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Beginner error I'd need help with

    Hi,

    Ups! I have not noticed!
    Òscar Llarch i Galán

Similar Threads

  1. Please help! I'm a beginner
    By ayanda83 in forum Newbie
    Replies: 2
    Last Post: 5th July 2012, 13:41
  2. Help! about Qt for beginner!!
    By Cantora in forum Newbie
    Replies: 9
    Last Post: 2nd April 2009, 00:11
  3. hello, a beginner would like your help
    By tommy in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2007, 22:48
  4. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 14:15
  5. QT4 beginner Fatal Error
    By Remyfr in forum Installation and Deployment
    Replies: 3
    Last Post: 11th March 2006, 01:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.