Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I need some exercises for each chapter
Qt 6.12 LTS is out! See what's new in the release blog

I need some exercises for each chapter

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 5 Posters 33.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT tomy

    Thanks to all.
    @jsulm
    So apparently you disagree with mrjj. I write what he says but the code shows nothing. I think your assumption is correct because myslot() has no parameters.

    Now the question is this: How to connect the buttons (1, 2 & +) to a slot so that, that slot prints out the values of these buttons onto the lineEdit please?

    Based on what I'd learned form C++ I thought the code below would work for that purpose:

    in the Calculator.h:

    ...
    private slots:
       void myslot(const QString&);
    };
    

    And in Calculator.cpp:

    ...
    connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot("1")));
     connect(twoButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot("2")));
     connect(addButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot("+")));
    }
    
    void Calculator::myslot(const QString& str){
     lineEdit -> setText(str);
    }
    

    But this does not work. I have a good experience in C++ but I'm a novice in Qt.
    Now what would be your solution for that purpose please?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #19

    @tomy Where did I disagree with @mrjj?
    You should read more carefully what I wrote.
    The problem with your code is that you provide a wrong slot signature in connect. Again:

    SLOT(myslot(bool))
    

    there is no such slot! Your slot has this signature (no bool parameter):

    void Calculator::myslot()
    

    So, either change

    SLOT(myslot(bool))
    to
    SLOT(myslot())
    

    or

    void Calculator::myslot()
    to
    void Calculator::myslot(bool)
    

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    tomyT 1 Reply Last reply
    1
    • jsulmJ jsulm

      @tomy Where did I disagree with @mrjj?
      You should read more carefully what I wrote.
      The problem with your code is that you provide a wrong slot signature in connect. Again:

      SLOT(myslot(bool))
      

      there is no such slot! Your slot has this signature (no bool parameter):

      void Calculator::myslot()
      

      So, either change

      SLOT(myslot(bool))
      to
      SLOT(myslot())
      

      or

      void Calculator::myslot()
      to
      void Calculator::myslot(bool)
      
      tomyT Offline
      tomyT Offline
      tomy
      wrote on last edited by
      #20

      @jsulm

      OK & thanks a bunch but still it doesn't work!

      1 Reply Last reply
      0
      • tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #21

        I appreciate your time that you all dedicate to help me but is it that hard to write a function that has a string parameter so that when we call it by strings it prints out the sent string onto the lineEdit?

        jsulmJ 1 Reply Last reply
        0
        • tomyT tomy

          I appreciate your time that you all dedicate to help me but is it that hard to write a function that has a string parameter so that when we call it by strings it prints out the sent string onto the lineEdit?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #22

          @tomy If you say "does not work" you should say what does not work or what happens.
          You should really read about signals/slots.

          You could use lambdas (C++11) and new connect syntax like this:

          connect(oneButton, &QPushButton::clicked, []() { lineEdit->setText("1"); });
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #23
            This post is deleted!
            1 Reply Last reply
            0
            • tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by tomy
              #24

              I meant using neither:

              connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot("1")));

              void Calculator::myslot(const QString& str){
              lineEdit -> setText(str);
              }
              Nor:

              connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));

              void Calculator::myslot(bool){
              lineEdit -> setText("1");
              }
              And Nor:

              connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot()));

              void Calculator::myslot(){
              lineEdit -> setText("1");
              }
              the program works!

              That is when I each time using the above methods run the code and click on the buttons, nothing will be printed out onto the lineEdit, that is clicking the buttons do nothing.

              Now you say I should read about:
              lambdas (C++11) and new connect syntax like this:
              connect(oneButton, &QPushButton::clicked, { lineEdit->setText("1"); });

              BUT THESE ARE VERY ADVANCED FOR ME. AT THE BEGINING OF THIS THREAD I SAID I NEED SOME PROPER EXERCISE FOR WHAT I HAVE LEARNED THROUGH THESE 2 CHAPTERS OF THAT BOOK, NOT ADVANCED TOPICS THAT ARE IN THE NEXT CHAPTERS OF THE BOOK.

              jsulmJ 1 Reply Last reply
              0
              • tomyT tomy

                I meant using neither:

                connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot("1")));

                void Calculator::myslot(const QString& str){
                lineEdit -> setText(str);
                }
                Nor:

                connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));

                void Calculator::myslot(bool){
                lineEdit -> setText("1");
                }
                And Nor:

                connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot()));

                void Calculator::myslot(){
                lineEdit -> setText("1");
                }
                the program works!

                That is when I each time using the above methods run the code and click on the buttons, nothing will be printed out onto the lineEdit, that is clicking the buttons do nothing.

                Now you say I should read about:
                lambdas (C++11) and new connect syntax like this:
                connect(oneButton, &QPushButton::clicked, { lineEdit->setText("1"); });

                BUT THESE ARE VERY ADVANCED FOR ME. AT THE BEGINING OF THIS THREAD I SAID I NEED SOME PROPER EXERCISE FOR WHAT I HAVE LEARNED THROUGH THESE 2 CHAPTERS OF THAT BOOK, NOT ADVANCED TOPICS THAT ARE IN THE NEXT CHAPTERS OF THE BOOK.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #25

                @tomy I already suggested to check what connect(...) returns to see whether it works or not. Did you try it?

                qDebug() << connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                

                it should print true if it succeeded.

                This connect call is wrong:

                connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                

                lineEdit is QLineEdit, right? QLineEdit does not have a slot named "myslot(bool)". This slot is in your Calculator class, right?
                Then it should be:

                connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot(bool)));
                

                connect() will print a warning if it cannot do the connection - you can check "Application output" tab in QtCreator.
                Furthermore you can try to debug: set a break point inside the slot run your app with debugger (F5 in QtCreator), press the button and see whether the slot is called or not.

                You wrote "I have a good experience in C++", so I assumed you're familiar with lambdas as they were introduced in C++11. And in your current use case they are very handy.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                tomyT 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @tomy I already suggested to check what connect(...) returns to see whether it works or not. Did you try it?

                  qDebug() << connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                  

                  it should print true if it succeeded.

                  This connect call is wrong:

                  connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                  

                  lineEdit is QLineEdit, right? QLineEdit does not have a slot named "myslot(bool)". This slot is in your Calculator class, right?
                  Then it should be:

                  connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot(bool)));
                  

                  connect() will print a warning if it cannot do the connection - you can check "Application output" tab in QtCreator.
                  Furthermore you can try to debug: set a break point inside the slot run your app with debugger (F5 in QtCreator), press the button and see whether the slot is called or not.

                  You wrote "I have a good experience in C++", so I assumed you're familiar with lambdas as they were introduced in C++11. And in your current use case they are very handy.

                  tomyT Offline
                  tomyT Offline
                  tomy
                  wrote on last edited by tomy
                  #26

                  @jsulm said in I need some exercises for each chapter:

                  @tomy I already suggested to check what connect(...) returns to see whether it works or not. Did you try it?

                  qDebug() << connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                  

                  it should print true if it succeeded.

                  This is the Calculator.cpp now:

                  #include <QtWidgets>
                  #include "calculator.h"
                  
                  Calculator::Calculator(QWidget *parent)
                      :QDialog(parent)
                  {
                      setupUi(this);
                  
                     qDebug() << connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot()));
                  }
                  
                  void Calculator::myslot(){
                      lineEdit -> setText("1");
                  }
                  

                  I ran it (Ctrl+R) but it show no errors and runs successfully. How to know what it has printed?

                  This connect call is wrong:

                  connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                  

                  lineEdit is QLineEdit, right? QLineEdit does not have a slot named "myslot(bool)". This slot is in your Calculator class, right?
                  Then it should be:

                  connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot(bool)));
                  

                  OK, I changed the connect to:

                  connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot()));
                  

                  But no changes again.

                  connect() will print a warning if it cannot do the connection - you can check "Application output" tab in QtCreator.

                  Application output is in the bottom, I checked it in both cases (above). There is no warning message there.

                  Furthermore you can try to debug: set a break point inside the slot run your app with debugger (F5 in QtCreator), press the button and see whether the slot is called or not.

                  I also tried this option. I pressed F5 for the two above cases. In the Application output this has been printed:
                  Debugging starts
                  Debugging has finished

                  You wrote "I have a good experience in C++", so I assumed you're familiar with lambdas as they were introduced in C++11. And in your current use case they are very handy.

                  Yes, I have a good experience in C++ but it's not 11. But do you the book I read for Qt? Do you know at what level (of Qt) is the reader of it?
                  I've passed the first two chapters and can start chapter 3, but, I don't like to deceive myself, rather, I want to examine myself to see whether I have learned the contents good enough or not.

                  jsulmJ 1 Reply Last reply
                  0
                  • tomyT tomy

                    @jsulm said in I need some exercises for each chapter:

                    @tomy I already suggested to check what connect(...) returns to see whether it works or not. Did you try it?

                    qDebug() << connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                    

                    it should print true if it succeeded.

                    This is the Calculator.cpp now:

                    #include <QtWidgets>
                    #include "calculator.h"
                    
                    Calculator::Calculator(QWidget *parent)
                        :QDialog(parent)
                    {
                        setupUi(this);
                    
                       qDebug() << connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot()));
                    }
                    
                    void Calculator::myslot(){
                        lineEdit -> setText("1");
                    }
                    

                    I ran it (Ctrl+R) but it show no errors and runs successfully. How to know what it has printed?

                    This connect call is wrong:

                    connect(oneButton,SIGNAL(clicked(bool)), lineEdit, SLOT(myslot(bool)));
                    

                    lineEdit is QLineEdit, right? QLineEdit does not have a slot named "myslot(bool)". This slot is in your Calculator class, right?
                    Then it should be:

                    connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot(bool)));
                    

                    OK, I changed the connect to:

                    connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot()));
                    

                    But no changes again.

                    connect() will print a warning if it cannot do the connection - you can check "Application output" tab in QtCreator.

                    Application output is in the bottom, I checked it in both cases (above). There is no warning message there.

                    Furthermore you can try to debug: set a break point inside the slot run your app with debugger (F5 in QtCreator), press the button and see whether the slot is called or not.

                    I also tried this option. I pressed F5 for the two above cases. In the Application output this has been printed:
                    Debugging starts
                    Debugging has finished

                    You wrote "I have a good experience in C++", so I assumed you're familiar with lambdas as they were introduced in C++11. And in your current use case they are very handy.

                    Yes, I have a good experience in C++ but it's not 11. But do you the book I read for Qt? Do you know at what level (of Qt) is the reader of it?
                    I've passed the first two chapters and can start chapter 3, but, I don't like to deceive myself, rather, I want to examine myself to see whether I have learned the contents good enough or not.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #27

                    @tomy said in I need some exercises for each chapter:

                    connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot()));

                    You should print the connect() return value:

                    qDebug() << connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot()));
                    

                    The output should be in "Application Output" tab in QtCreator when you start the application.

                    To debug you need first to set a breakpoint inside your slot and then start with F5, then press the button.

                    Another thing to check is whether "oneButton" is really the button you see when you start your application. Usually all UI objects are accessed via ui->oneButton. But you decided to do it differently. Since I don't have your complete code I cannot check this.

                    "But do you the book I read for Qt? Do you know at what level (of Qt) is the reader of it?" - no I don't. I just try to help you. Lambdas are not Qt, they are pure C++11. The only new Qt thing for you is the new connect() syntax. In my opinion it is not wrong to learn something new during exercises even if it was not yet covered by the book. But if you don't want to then just ignore the suggested lambda based solution.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by
                      #28

                      First I know you want to help me and for that I appreciate you and thank you.

                      You also are right about learning new things during solving a given exercise but I think the way is improper. It's probably my fault either.

                      Anyway, we all (in this thread) want to solve the issue. I have a suggestion! Design a simplest form that is possible to do a single work please. That is, a form with one button and one lineEdit, when you run it and click on the button it will be printed on the lineEdit.
                      To sum up, a form named (say) "test" with a test.h, test.cpp and a main.cpp file in the most simple style/method (that is, suitable for low level beginner of Qt).

                      I think that way most of the issues will be solved.

                      jsulmJ 1 Reply Last reply
                      0
                      • tomyT tomy

                        First I know you want to help me and for that I appreciate you and thank you.

                        You also are right about learning new things during solving a given exercise but I think the way is improper. It's probably my fault either.

                        Anyway, we all (in this thread) want to solve the issue. I have a suggestion! Design a simplest form that is possible to do a single work please. That is, a form with one button and one lineEdit, when you run it and click on the button it will be printed on the lineEdit.
                        To sum up, a form named (say) "test" with a test.h, test.cpp and a main.cpp file in the most simple style/method (that is, suitable for low level beginner of Qt).

                        I think that way most of the issues will be solved.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #29

                        @tomy This works for me just fine.
                        ButtonEdit.pro file:

                        QT       += core gui
                        
                        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                        
                        TARGET = ButtonEdit
                        TEMPLATE = app
                        
                        
                        SOURCES += main.cpp\
                                mainwindow.cpp
                        
                        HEADERS  += mainwindow.h
                        
                        FORMS    += mainwindow.ui
                        

                        mainwindow.h

                        #ifndef MAINWINDOW_H
                        #define MAINWINDOW_H
                        
                        #include <QMainWindow>
                        
                        namespace Ui {
                        class MainWindow;
                        }
                        
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            explicit MainWindow(QWidget *parent = 0);
                            ~MainWindow();
                        
                        private slots:
                            void mySlot();
                        
                        private:
                            Ui::MainWindow *ui;
                        };
                        
                        #endif // MAINWINDOW_H
                        

                        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(mySlot()));
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        
                        void MainWindow::mySlot()
                        {
                            ui->lineEdit->setText("+");
                        

                        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.ui

                        <?xml version="1.0" encoding="UTF-8"?>
                        <ui version="4.0">
                         <class>MainWindow</class>
                         <widget class="QMainWindow" name="MainWindow">
                          <property name="geometry">
                           <rect>
                            <x>0</x>
                            <y>0</y>
                            <width>400</width>
                            <height>300</height>
                           </rect>
                          </property>
                          <property name="windowTitle">
                           <string>MainWindow</string>
                          </property>
                          <widget class="QWidget" name="centralWidget">
                           <widget class="QPushButton" name="pushButton">
                            <property name="geometry">
                             <rect>
                              <x>10</x>
                              <y>10</y>
                              <width>99</width>
                              <height>27</height>
                             </rect>
                            </property>
                            <property name="text">
                             <string>+</string>
                            </property>
                           </widget>
                           <widget class="QLineEdit" name="lineEdit">
                            <property name="geometry">
                             <rect>
                              <x>10</x>
                              <y>50</y>
                              <width>113</width>
                              <height>27</height>
                             </rect>
                            </property>
                           </widget>
                          </widget>
                         </widget>
                         <layoutdefault spacing="6" margin="11"/>
                         <resources/>
                         <connections/>
                        </ui>
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        tomyT 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @tomy This works for me just fine.
                          ButtonEdit.pro file:

                          QT       += core gui
                          
                          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                          
                          TARGET = ButtonEdit
                          TEMPLATE = app
                          
                          
                          SOURCES += main.cpp\
                                  mainwindow.cpp
                          
                          HEADERS  += mainwindow.h
                          
                          FORMS    += mainwindow.ui
                          

                          mainwindow.h

                          #ifndef MAINWINDOW_H
                          #define MAINWINDOW_H
                          
                          #include <QMainWindow>
                          
                          namespace Ui {
                          class MainWindow;
                          }
                          
                          class MainWindow : public QMainWindow
                          {
                              Q_OBJECT
                          
                          public:
                              explicit MainWindow(QWidget *parent = 0);
                              ~MainWindow();
                          
                          private slots:
                              void mySlot();
                          
                          private:
                              Ui::MainWindow *ui;
                          };
                          
                          #endif // MAINWINDOW_H
                          

                          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(mySlot()));
                          }
                          
                          MainWindow::~MainWindow()
                          {
                              delete ui;
                          }
                          
                          void MainWindow::mySlot()
                          {
                              ui->lineEdit->setText("+");
                          

                          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.ui

                          <?xml version="1.0" encoding="UTF-8"?>
                          <ui version="4.0">
                           <class>MainWindow</class>
                           <widget class="QMainWindow" name="MainWindow">
                            <property name="geometry">
                             <rect>
                              <x>0</x>
                              <y>0</y>
                              <width>400</width>
                              <height>300</height>
                             </rect>
                            </property>
                            <property name="windowTitle">
                             <string>MainWindow</string>
                            </property>
                            <widget class="QWidget" name="centralWidget">
                             <widget class="QPushButton" name="pushButton">
                              <property name="geometry">
                               <rect>
                                <x>10</x>
                                <y>10</y>
                                <width>99</width>
                                <height>27</height>
                               </rect>
                              </property>
                              <property name="text">
                               <string>+</string>
                              </property>
                             </widget>
                             <widget class="QLineEdit" name="lineEdit">
                              <property name="geometry">
                               <rect>
                                <x>10</x>
                                <y>50</y>
                                <width>113</width>
                                <height>27</height>
                               </rect>
                              </property>
                             </widget>
                            </widget>
                           </widget>
                           <layoutdefault spacing="6" margin="11"/>
                           <resources/>
                           <connections/>
                          </ui>
                          
                          tomyT Offline
                          tomyT Offline
                          tomy
                          wrote on last edited by
                          #30

                          @jsulm
                          Thank you. Would you please take a screenshot of the design page (mainwindow.ui) and send it?

                          jsulmJ 1 Reply Last reply
                          0
                          • tomyT tomy

                            @jsulm
                            Thank you. Would you please take a screenshot of the design page (mainwindow.ui) and send it?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #31

                            @tomy I could, but there is nothing special: just a button and a line edit bellow the button.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • tomyT Offline
                              tomyT Offline
                              tomy
                              wrote on last edited by
                              #32

                              It couldn't help. But I thank you and appreciate your time. The thread is finished.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved