Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Some problem to learn Qt :(
Forum Updated to NodeBB v4.3 + New Features

Some problem to learn Qt :(

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    Hi, I'm studying and sperimenting C++ and Qt.
    To make it, I've developed a little program and obviously I've encountered some problems.
    The program now is finished and running, but some problems are unresolved.
    For example, I haven't been able to change value to "lcdNumber" display from another class file.
    I haven't understood scope of these.
    For example, to try to understand the problem I created a little program.
    It is composed from a window with one "lcdNumber", and two "pushButtons".
    One pushButton set display to zero, another pushButton increments display to 1000.
    The first push button works fine because instruction (ui->lcdNumber.display(0)) is in mainwindows.cpp file, but increment button don't works because it calls a method that exist in another file cpp.

    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 "incdec.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        ui->lcdNumber->display(0);
    }
    
    void MainWindow::on_pushButton_3_clicked()
    {
        incdec  i;
        i.increment();
    }
    

    INCDEC.CPP

    #include "incdec.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    incdec::incdec()
    {
    
    }
    
    
    incdec::~incdec()
    {
    
    }
    
    void incdec::increment(void)
    {
        for (int i=0; i < 1000; i++)
            ui->lcdNumber.display(i);
    
    }
    
    void incdec::decrement(void)
    {
        
    
    }
    
    

    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 on_pushButton_clicked();
    
        void on_pushButton_3_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    INCDEC.H

    #include "incdec.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    incdec::incdec()
    {
    
    }
    
    
    incdec::~incdec()
    {
    
    }
    
    void incdec::increment(void)
    {
        for (int i=0; i < 1000; i++)
            ui->lcdNumber.display(i);
    }
    

    If I try to compile this program, I obtain this error: 'ui' was not declared in this scope.

    I've in my head a lot of confusion.
    Anyone can help me?

    Thanks.

    Stefano

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      ui belongs to MainWindow and not to incdec. If you want your incdec class to modify something in MainWindow, use signals and slots. Otherwise you are going to create tight couplings between your classes and it's a Bad Thing (™).

      Also, if you want to actually see the incrementation happening, the loop you are using is not the correct technique. It will run from 0 to 999 without letting the event loop run so at best you'll see 999 when done.

      You should take the time to got throughout Qt's documentation tutorial and examples.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Stefanoxjx
        wrote on last edited by
        #3

        Hi Sgaist and thanks for reply.

        ui belongs to MainWindow and not to incdec. If you want your incdec class to modify something in MainWindow, use signals and slots. Otherwise you are going to create tight couplings between your classes and it's a Bad Thing (™).
        

        Thanks for clarification

        
        Also, if you want to actually see the incrementation happening, the loop you are using is not the correct technique. It will run from 0 to 999 without letting the event loop run so at best you'll see 999 when done.
        

        yes, I thought this too, but for the moment it isn't a problem

        You should take the time to got throughout Qt's documentation tutorial and examples.
        

        I searched examples width code how to this, but haven't found.
        Do you have a link to suggest me?

        Thanks.

        Stefano

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          hi
          for signals and slot, this is a must read
          http://doc.qt.io/qt-5/signalsandslots.html

          And i made sample for you with dialog that emit signal to mainwindow and
          it updates the lcdnumber.

          https://www.dropbox.com/s/zm7nd5pk7bmgkxp/signalfromdialog.zip?dl=0
          look for //key in source. this is places that its important for the signal & slot working.

          1 Reply Last reply
          1
          • Andy314A Offline
            Andy314A Offline
            Andy314
            wrote on last edited by
            #5

            The signal/slot mechanism is indeed the clean way.

            The other (not clean) way is to couple the dialogs. Because of the forward declaration of the UIs the concrete ui is normally only known in the CPPs.
            You must include the ui_SpecificDialog.h everywhere you need it. Then you can give a dialog the the function Ui_SpecificDialog* getUI() .

            As I said, this is not clean, but this info it helps to understand how Qt works.

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Stefanoxjx
              wrote on last edited by
              #6

              Hi at all.
              @Sgaist and @andy314 many thanks for clarification.
              @MRJJ many thanks for link and sample.
              Now I'm sperimenting on it to learn how signal/slot works.
              Many thanks at all.

              Stefano

              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 Extensions
              • Unsolved