namespace doubts in program



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


  • Moderators

    @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


  • jsulm, Thank you.

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

  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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



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


  • Qt Champions 2016

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



  • @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?


  • Moderators

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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);



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



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

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



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


  • Qt Champions 2016

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



  • @mrjj
    Thanks to all for pointing in right direction.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.