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. namespace doubts in program
Forum Updated to NodeBB v4.3 + New Features

namespace doubts in program

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 4.3k Views 1 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.
  • O o6a6r9v1p

    we have a program, and its file structure is given below:

    @app.h


    //end of app.h
    @

    @app.cpp


    //end of app.cpp
    @

    @demoapp.h
    #include "app.h"

    namespace Ui { class DemoApp }
    class DemoApp:public QMainWindow
    {
    explicit DemoApp(QWidget *parent=0);
    ~DemoApp();

    private:
    Ui::DemoApp *ui;

    slots:

    signals:

    };
    //end of demoapp.h
    @

    @demoapp.cpp
    #include "demoapp.h"
    #include "ui_demoapp.h"

    DemoApp::DemoApp(QWidget *parent=0) :QMainWindow(parent, ui(new Ui::DemoApp)
    {ui->setupUi(this);


    }
    DemoApp::~DemoApp()
    {

    }
    //Other functions here
    //end of demoapp.cpp
    @

    @ui_demoapp.h

    QT_BEGIN_NAMESPACE
    class Ui_DemoApp
    {
    void setupUi(QMainWindow *DemoApp)
    {
    ------
    retranslateUi(DemoApp);
    QMetaObject::connectSlotsByName(DemoApp);
    }

    void retranslateUi(QMainWindow *DemoApp)
    {
    
    }
    

    namespace Ui{
    class DemoAPP: public Ui_DemoApp{};
    }

    }
    //end of ui_demoapp.h
    @

    @main.cpp
    #include "demoapp.h"

    int main ()
    {

    DemoApp w;
    w.show();
    return a.exec();

    }
    //end of main.cpp
    @

    1. namespace "Ui" is created in "demoapp.h" & some other code created in "ui_demoapp.h" and is also added to name space "Ui".

    2. "*ui" is pointer to "Ui::DemoApp" , means ui points to DemoApp class in name space Ui. Am i correct?

    3. ui->setupUi(this); this function is defined in another file "ui_demoapp.h" along with retranslateUi() function.
      Can we move these two functions to "demoApp.cpp" file where "DemoApp" class functions are defined.
      what changes are to be done?

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

    @o6a6r9v1p

    1. Yes, this is how Qt handles auto-generated code (moc)
    2. No. A pointer cannot point to a class, it points to an instance of a class (to an instance of Ui::DemoApp in this case)
    3. Don't do that! setupUi() and retranslateUi() is auto-generated code and will change every-time you change your UI! There is just no need to move it to demoApp.cpp

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

    1 Reply Last reply
    2
    • O Offline
      O Offline
      o6a6r9v1p
      wrote on last edited by
      #3

      jsulm, Thank you.
      2) it is instance of Ui::DemoApp , pointed by ui.
      3) Can I add/modify the code in setupUi() function. How can I add some Widgets to it manually.

      mrjjM 1 Reply Last reply
      0
      • O o6a6r9v1p

        jsulm, Thank you.
        2) it is instance of Ui::DemoApp , pointed by ui.
        3) Can I add/modify the code in setupUi() function. How can I add some Widgets to it manually.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #4

        @o6a6r9v1p said in namespace doubts in program:

        Can I add/modify the code in setupUi() function. How can I add some Widgets to it manually.

        after setupUI() is being called, you can add new widgets to the existing
        layout etc.
        All is accessible via the ui->widgetname so that's how u can get access to place holder/layouts etc.
        So you just do it after the automatic part.

        1 Reply Last reply
        2
        • O Offline
          O Offline
          o6a6r9v1p
          wrote on last edited by
          #5

          mrjj,
          shall i do it inside setupui() function or
          after setup() is called in DemoApp constructor in demoapp.cpp file?

          mrjjM 1 Reply Last reply
          0
          • O o6a6r9v1p

            mrjj,
            shall i do it inside setupui() function or
            after setup() is called in DemoApp constructor in demoapp.cpp file?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #6

            @o6a6r9v1p
            Always AFTER!
            The setupui() is generated. Do not change that file. it will be overwritten suddenly.
            All ui_xxxx files are not to be edited. :)

            And it dont have to be in the constructor . Can be any time after.

            1 Reply Last reply
            2
            • O Offline
              O Offline
              o6a6r9v1p
              wrote on last edited by
              #7

              mrjj,
              Thank you for the advice. I will do accordingly and would comeback with result.

              1 Reply Last reply
              1
              • O Offline
                O Offline
                o6a6r9v1p
                wrote on last edited by
                #8

                mrjj,
                "And it dont have to be in the constructor . Can be any time after."
                Where can I put the additional code, out side Constructor? any Clue!

                mrjjM 1 Reply Last reply
                0
                • O o6a6r9v1p

                  mrjj,
                  "And it dont have to be in the constructor . Can be any time after."
                  Where can I put the additional code, out side Constructor? any Clue!

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @o6a6r9v1p

                  Well i just have a function in mainwindow. I call from constructor.
                  But what i mean is , you can also have
                  a button that then add new widgets to the interface.
                  And so on.
                  So use a function as not to make constructor big or directly on button / where u need it.

                  O 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @o6a6r9v1p

                    Well i just have a function in mainwindow. I call from constructor.
                    But what i mean is , you can also have
                    a button that then add new widgets to the interface.
                    And so on.
                    So use a function as not to make constructor big or directly on button / where u need it.

                    O Offline
                    O Offline
                    o6a6r9v1p
                    wrote on last edited by
                    #10

                    @mrjj
                    I did as you told.

                    The DemoApp Constructor is:
                    @
                    DemoApp::DemoApp(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::DemoApp)
                    {
                    ui->setupUi(this);

                    modifyGui(this); //this function is added now
                    
                    
                    connect(this, SIGNAL(----), ----, SLOT(-----));
                    connect(-----, SIGNAL(----)), this, SLOT(----));
                    

                    }
                    @

                    @
                    void modifyGui(QMainWindow *DemoApp)
                    {

                    --

                    }
                    @
                    is defined in main.cpp

                    I am getting the error : "modifyGui" was not declared in this scope
                    where shall I put this function?

                    jsulmJ 1 Reply Last reply
                    0
                    • O o6a6r9v1p

                      @mrjj
                      I did as you told.

                      The DemoApp Constructor is:
                      @
                      DemoApp::DemoApp(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::DemoApp)
                      {
                      ui->setupUi(this);

                      modifyGui(this); //this function is added now
                      
                      
                      connect(this, SIGNAL(----), ----, SLOT(-----));
                      connect(-----, SIGNAL(----)), this, SLOT(----));
                      

                      }
                      @

                      @
                      void modifyGui(QMainWindow *DemoApp)
                      {

                      --

                      }
                      @
                      is defined in main.cpp

                      I am getting the error : "modifyGui" was not declared in this scope
                      where shall I put this function?

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

                      @o6a6r9v1p Why is modifyGui in main.cpp?
                      It must be a method in DemoApp as it is handling its content. Add modifyGui declaration to DemoApp class in demoapp.h and the definition to demoapp.cpp:

                      void DemoApp::modifyGui()
                      {
                      ...
                      }
                      

                      Also you do not need to pass any pointer to modifyGui if it is member of DemoApp.

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

                      O 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @o6a6r9v1p Why is modifyGui in main.cpp?
                        It must be a method in DemoApp as it is handling its content. Add modifyGui declaration to DemoApp class in demoapp.h and the definition to demoapp.cpp:

                        void DemoApp::modifyGui()
                        {
                        ...
                        }
                        

                        Also you do not need to pass any pointer to modifyGui if it is member of DemoApp.

                        O Offline
                        O Offline
                        o6a6r9v1p
                        wrote on last edited by
                        #12

                        @jsulm
                        Did as you told. It is now compiling.

                        when I added widgets, they are not visible in GUI. do i modify any property ( I used Label, linedit widgets)
                        How to use other classes, for example, code from others etc. in my code. What precautions are to be taken

                        mrjjM 1 Reply Last reply
                        0
                        • O o6a6r9v1p

                          @jsulm
                          Did as you told. It is now compiling.

                          when I added widgets, they are not visible in GUI. do i modify any property ( I used Label, linedit widgets)
                          How to use other classes, for example, code from others etc. in my code. What precautions are to be taken

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @o6a6r9v1p

                          Hi after you create a new Widget (with parent)
                          you might need to call show.

                          Or it just a bug and you give it to wrong parent and its covered under something.

                          So please show code on how you create and insert the new widgets.

                          O 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @o6a6r9v1p

                            Hi after you create a new Widget (with parent)
                            you might need to call show.

                            Or it just a bug and you give it to wrong parent and its covered under something.

                            So please show code on how you create and insert the new widgets.

                            O Offline
                            O Offline
                            o6a6r9v1p
                            wrote on last edited by
                            #14

                            @mrjj
                            Code for ModifyGui() is
                            @
                            void DemoApp::modifyGui()
                            {

                            QLabel *label_Status1 = new QLabel(tr("Status1"));
                            label_Status1->setObjectName(QString::fromUtf8("Status1"));
                            label_Status1->setEnabled(true);
                            label_Status1->setGeometry(QRect(410, 10, 41, 21));
                            label_Status1->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
                            QLineEdit *deviceStatus1 = new QLineEdit();
                            deviceStatus1->setObjectName(QString::fromUtf8("deviceStatus1"));
                            deviceStatus1->setEnabled(true);
                            deviceStatus1->setGeometry(QRect(20, 10, 391, 20));
                            deviceStatus1->setReadOnly(true);
                            
                            QHBoxLayout *statusLayout = new QHBoxLayout;
                            statusLayout->addWidget(deviceStatus1);
                            statusLayout->addWidget(label_Status1);
                            
                            QVBoxLayout *mainLayout = new QVBoxLayout;
                            

                            // mainLayout->addLayout(statusLayout);
                            setLayout(mainLayout);

                            setWindowTitle(tr(" Device Monitor"));
                            

                            }

                            @

                            when i uncomment the commented line, I am getting error
                            "no matching function ".
                            little confused with call sequence or format in Qt.

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

                              It seems you try to add layout to layout ?

                              Make sure its the right type
                              http://doc.qt.io/qt-5/qgridlayout.html#addLayout
                              or
                              http://doc.qt.io/qt-5/qboxlayout.html#addLayout
                              or ?
                              and to include the other parameters depending of the type of mainLayout

                              Alternatively , use a place holder widget
                              Apply statusLayout to this widget and
                              mainLayout->addWidget(place_holder);

                              1 Reply Last reply
                              1
                              • O Offline
                                O Offline
                                o6a6r9v1p
                                wrote on last edited by
                                #16

                                Hi mrjj,
                                This is the code for setupGui() & retranslate Gui()

                                @
                                void setupUi(QMainWindow *DemoApp)
                                {
                                if (DemoApp->objectName().isEmpty())
                                DemoApp->setObjectName(QString::fromUtf8("DemoApp"));
                                DemoApp->resize(600, 400);
                                centralWidget = new QWidget(DemoApp);
                                centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
                                progressBar = new QProgressBar(centralWidget);
                                progressBar->setObjectName(QString::fromUtf8("progressBar"));
                                progressBar->setEnabled(false);
                                progressBar->setGeometry(QRect(20, 100, 421, 21));
                                progressBar->setMaximum(1024);
                                progressBar->setValue(0);
                                progressBar->setTextVisible(false);
                                pushButton = new QPushButton(centralWidget);
                                pushButton->setObjectName(QString::fromUtf8("pushButton"));
                                pushButton->setEnabled(false);
                                pushButton->setGeometry(QRect(20, 50, 91, 21));
                                pushbuttonStatus = new QLabel(centralWidget);
                                pushbuttonStatus->setObjectName(QString::fromUtf8("pushbuttonStatus"));
                                pushbuttonStatus->setEnabled(false);
                                pushbuttonStatus->setGeometry(QRect(200, 50, 231, 20));
                                pushbuttonStatus->setCursor(QCursor(Qt::ArrowCursor));
                                pushbuttonStatus->setInputMethodHints(Qt::ImhNone);
                                pushbuttonStatus->setFrameShadow(QFrame::Plain);
                                pushbuttonStatus->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
                                label_2 = new QLabel(centralWidget);
                                label_2->setObjectName(QString::fromUtf8("label_2"));
                                label_2->setEnabled(false);
                                label_2->setGeometry(QRect(30, 80, 401, 20));
                                label_2->setAlignment(Qt::AlignCenter);
                                label_3 = new QLabel(centralWidget);
                                label_3->setObjectName(QString::fromUtf8("label_3"));
                                label_3->setEnabled(true);
                                label_3->setGeometry(QRect(410, 10, 41, 21));
                                label_3->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
                                deviceStatus = new QLineEdit(centralWidget);
                                deviceStatus->setObjectName(QString::fromUtf8("deviceStatus"));
                                deviceStatus->setEnabled(true);
                                deviceStatus->setGeometry(QRect(20, 10, 391, 20));
                                deviceStatus->setReadOnly(true);

                                    DemoApp->setCentralWidget(centralWidget);
                                    menuBar = new QMenuBar(DemoApp);
                                    menuBar->setObjectName(QString::fromUtf8("menuBar"));
                                    menuBar->setGeometry(QRect(0, 0, 461, 23));
                                    DemoApp->setMenuBar(menuBar);
                                    statusBar = new QStatusBar(DemoApp);
                                    statusBar->setObjectName(QString::fromUtf8("statusBar"));
                                    DemoApp->setStatusBar(statusBar);
                                
                                    retranslateUi(DemoApp);
                                
                                    QMetaObject::connectSlotsByName(DemoApp);
                                } // setupUi
                                

                                @

                                @ void retranslateUi(QMainWindow *DemoApp)
                                {
                                DemoApp->setWindowTitle(QApplication::translate("DemoApp", "DemoApp", 0));
                                pushButton->setText(QApplication::translate("DemoApp", "ToggleLED(s)", 0));
                                pushbuttonStatus->setText(QApplication::translate("DemoApp", "Pushbutton State: Pressed", 0));
                                label_2->setText(QApplication::translate("DemoApp", "Not Pressed", 0));
                                label_3->setText(QApplication::translate("DemoApp", "Status", 0));
                                deviceStatus->setText(QApplication::translate("DemoApp", "Device Not Detected: Verify Connection/Correct Firmware", 0));
                                } // retranslateUi
                                @

                                1 Reply Last reply
                                0
                                • O Offline
                                  O Offline
                                  o6a6r9v1p
                                  wrote on last edited by
                                  #17

                                  Sorry Typing mistake. They are setupUi() and retranslateUi().

                                  It means we have to add widgets to centralwindow.
                                  thank you.

                                  1 Reply Last reply
                                  0
                                  • O Offline
                                    O Offline
                                    o6a6r9v1p
                                    wrote on last edited by
                                    #18

                                    modifyGui() is changed as told by you.

                                    @void DemoApp::modifyGui()
                                    {

                                    QLabel *label_Status1 = new QLabel(tr("Status1"));
                                    label_Status1->setObjectName(QString::fromUtf8("Status1"));
                                    label_Status1->setEnabled(true);
                                    label_Status1->setGeometry(QRect(410, 10, 41, 21));
                                    label_Status1->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
                                    QLineEdit *deviceStatus1 = new QLineEdit();
                                    deviceStatus1->setObjectName(QString::fromUtf8("deviceStatus1"));
                                    deviceStatus1->setEnabled(true);
                                    deviceStatus1->setGeometry(QRect(20, 10, 391, 20));
                                    deviceStatus1->setReadOnly(true);
                                    
                                    QHBoxLayout *statusLayout = new QHBoxLayout;
                                    statusLayout->addWidget(deviceStatus1);
                                    statusLayout->addWidget(label_Status1);
                                    

                                    QWidget *window = new QWidget();
                                    window->setLayout(statusLayout);
                                    setCentralWidget(window);

                                    }
                                    @

                                    It is showing GUI, but it stops working,
                                    it gives the message "The program has unexpectedly finished."
                                    what may be the problem?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • O o6a6r9v1p

                                      modifyGui() is changed as told by you.

                                      @void DemoApp::modifyGui()
                                      {

                                      QLabel *label_Status1 = new QLabel(tr("Status1"));
                                      label_Status1->setObjectName(QString::fromUtf8("Status1"));
                                      label_Status1->setEnabled(true);
                                      label_Status1->setGeometry(QRect(410, 10, 41, 21));
                                      label_Status1->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
                                      QLineEdit *deviceStatus1 = new QLineEdit();
                                      deviceStatus1->setObjectName(QString::fromUtf8("deviceStatus1"));
                                      deviceStatus1->setEnabled(true);
                                      deviceStatus1->setGeometry(QRect(20, 10, 391, 20));
                                      deviceStatus1->setReadOnly(true);
                                      
                                      QHBoxLayout *statusLayout = new QHBoxLayout;
                                      statusLayout->addWidget(deviceStatus1);
                                      statusLayout->addWidget(label_Status1);
                                      

                                      QWidget *window = new QWidget();
                                      window->setLayout(statusLayout);
                                      setCentralWidget(window);

                                      }
                                      @

                                      It is showing GUI, but it stops working,
                                      it gives the message "The program has unexpectedly finished."
                                      what may be the problem?

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      @o6a6r9v1p said in namespace doubts in program:

                                      "The program has unexpectedly finished."

                                      It means it crash.
                                      Fastest way is to set some breakpoints and
                                      single step between them to try find the line that makes crash.

                                      Nothing springs to eye for crashing in that code.

                                      O 1 Reply Last reply
                                      1
                                      • mrjjM mrjj

                                        @o6a6r9v1p said in namespace doubts in program:

                                        "The program has unexpectedly finished."

                                        It means it crash.
                                        Fastest way is to set some breakpoints and
                                        single step between them to try find the line that makes crash.

                                        Nothing springs to eye for crashing in that code.

                                        O Offline
                                        O Offline
                                        o6a6r9v1p
                                        wrote on last edited by
                                        #20

                                        @mrjj
                                        Thanks to all for pointing in right direction.

                                        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