Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Some problem to learn Qt :(



  • 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


  • Lifetime Qt Champion

    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.



  • 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


  • Lifetime Qt Champion

    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.



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



  • 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


Log in to reply