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. Added widgets manually to Qt generated code, shown in 2nd window
Forum Updated to NodeBB v4.3 + New Features

Added widgets manually to Qt generated code, shown in 2nd window

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 4.1k 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 Offline
    O Offline
    o6a6r9v1p
    wrote on last edited by koahnig
    #1

    Code for the program is

    DemoApp::DemoApp(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::DemoApp)
    {
        ui->setupUi(this);
       
        extendGui();
       
    }
    
    void DemoApp::extendGui()
    {
    
        QLabel *label_Status = new QLabel();
        label_Status->setObjectName(QString::fromUtf8("Stat3"));
        label_Status->setEnabled(true);
        label_Status->setGeometry(QRect(410, 10, 41, 21));
        label_Status->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
        QLineEdit *deviceLabel = new QLineEdit();
        deviceLabel->setObjectName(QString::fromUtf8("deviceLabel"));
        deviceLabel->setEnabled(true);
        deviceLabel->setGeometry(QRect(20, 10, 391, 20));
        deviceLabel->setReadOnly(true);
    
        QHBoxLayout *deviceLayout = new QHBoxLayout;
        deviceLayout->addWidget(deviceLabel);
        deviceLayout->addWidget(label_Status);
      
                QWidget *w = new QWidget();
                w->setLayout(deviceLayout); 
                w->setWindowTitle("layouts");
                w->show();
    }
    

    some widgets are created in setupUi(), by Qt creator, to add additonal functionality
    some widgets are added in constructor using extendGui() function.

    1. widgets added in extendGui() are being displayed in another window, "label_Status" is missing.
    2. how to make Layout to automatically adjust size.

    what is the mistake in the program.

    [edit:koahnig] Code tags corrected

    jsulmJ 1 Reply Last reply
    0
    • Venkatesh VV Offline
      Venkatesh VV Offline
      Venkatesh V
      wrote on last edited by
      #2

      Hi..
      If you want to show everything in single window then you should set parent to w (this) object otherwise it will become another widget.

      O 1 Reply Last reply
      5
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Your function is creating Separate widget and showing. It will be in separate window only. I suggest u to create designer window separately. Create widget separately. Add both of them to vertical or horizontal layout. Set the layout to another blank widget. Show this widget.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        6
        • Venkatesh VV Venkatesh V

          Hi..
          If you want to show everything in single window then you should set parent to w (this) object otherwise it will become another widget.

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

          @Venkatesh-V
          Hi,
          How to do this(setting to parent)

          jsulmJ Venkatesh VV 2 Replies Last reply
          0
          • O o6a6r9v1p

            Code for the program is

            DemoApp::DemoApp(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::DemoApp)
            {
                ui->setupUi(this);
               
                extendGui();
               
            }
            
            void DemoApp::extendGui()
            {
            
                QLabel *label_Status = new QLabel();
                label_Status->setObjectName(QString::fromUtf8("Stat3"));
                label_Status->setEnabled(true);
                label_Status->setGeometry(QRect(410, 10, 41, 21));
                label_Status->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
                QLineEdit *deviceLabel = new QLineEdit();
                deviceLabel->setObjectName(QString::fromUtf8("deviceLabel"));
                deviceLabel->setEnabled(true);
                deviceLabel->setGeometry(QRect(20, 10, 391, 20));
                deviceLabel->setReadOnly(true);
            
                QHBoxLayout *deviceLayout = new QHBoxLayout;
                deviceLayout->addWidget(deviceLabel);
                deviceLayout->addWidget(label_Status);
              
                        QWidget *w = new QWidget();
                        w->setLayout(deviceLayout); 
                        w->setWindowTitle("layouts");
                        w->show();
            }
            

            some widgets are created in setupUi(), by Qt creator, to add additonal functionality
            some widgets are added in constructor using extendGui() function.

            1. widgets added in extendGui() are being displayed in another window, "label_Status" is missing.
            2. how to make Layout to automatically adjust size.

            what is the mistake in the program.

            [edit:koahnig] Code tags corrected

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

            @o6a6r9v1p You did not add w widget to any layout of your DemoApp.

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

            1 Reply Last reply
            1
            • O o6a6r9v1p

              @Venkatesh-V
              Hi,
              How to do this(setting to parent)

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

              @o6a6r9v1p

              QWidget *w = new QWidget(this);
              

              but you still have to add it to a layout in your DemoApp.

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

              O 1 Reply Last reply
              1
              • O o6a6r9v1p

                @Venkatesh-V
                Hi,
                How to do this(setting to parent)

                Venkatesh VV Offline
                Venkatesh VV Offline
                Venkatesh V
                wrote on last edited by
                #7

                @o6a6r9v1p
                Qwidget *w = new QWidget(this);

                1 Reply Last reply
                3
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  "label_Status" is missing"

                  label_Status is empty, it has neither a picture nor a text and is set to a white or transparent background by default.

                  add

                  label_Status->setStyleSheet("background-color:red;");
                  

                  and you'll see a red line where your QLabel is in the layout.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  3
                  • jsulmJ jsulm

                    @o6a6r9v1p

                    QWidget *w = new QWidget(this);
                    

                    but you still have to add it to a layout in your DemoApp.

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

                    @jsulm said in Added widgets manually to Qt generated code, shown in 2nd window:

                    @o6a6r9v1p

                    QWidget *w = new QWidget(this);
                    

                    but you still have to add it to a layout in your DemoApp.

                    Main GUI is simple with a pushbutton and Line edit. There is no layout in it. Then how to add w to a layout.

                    jsulmJ 1 Reply Last reply
                    0
                    • O o6a6r9v1p

                      @jsulm said in Added widgets manually to Qt generated code, shown in 2nd window:

                      @o6a6r9v1p

                      QWidget *w = new QWidget(this);
                      

                      but you still have to add it to a layout in your DemoApp.

                      Main GUI is simple with a pushbutton and Line edit. There is no layout in it. Then how to add w to a layout.

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

                      @o6a6r9v1p First, add a layout to MainGui (in designer), then add your widget to that layout (you can get it using http://doc.qt.io/qt-5/qwidget.html#layout).

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

                      O 1 Reply Last reply
                      2
                      • jsulmJ jsulm

                        @o6a6r9v1p First, add a layout to MainGui (in designer), then add your widget to that layout (you can get it using http://doc.qt.io/qt-5/qwidget.html#layout).

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

                        @jsulm
                        Now i am getting only Main window, it seems the added/new widgets are not shown.

                        when i added layout to existing/old widgets , declared in ui_*.h, program is crashing.

                        jsulmJ 1 Reply Last reply
                        0
                        • O o6a6r9v1p

                          @jsulm
                          Now i am getting only Main window, it seems the added/new widgets are not shown.

                          when i added layout to existing/old widgets , declared in ui_*.h, program is crashing.

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

                          @o6a6r9v1p You should add the layout in designer. Can you show the code? Just saying it is crashing does not help to solve the problem.

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

                          O 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @o6a6r9v1p You should add the layout in designer. Can you show the code? Just saying it is crashing does not help to solve the problem.

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

                            @jsulm
                            added layout in designer, code is changed.
                            Code snippets are given below:
                            @

                            QT_BEGIN_NAMESPACE

                            class Ui_DemoApp
                            {
                            public:
                            QWidget *centralWidget;
                            QProgressBar *progressBar;
                            QPushButton *pushButton;
                            QLabel *label_3;
                            QLineEdit *deviceStatus;
                            QMenuBar *menuBar;
                            QStatusBar *statusBar;
                            QHBoxLayout *horizontalLayout;
                            QHBoxLayout *horizontalLayout_2;
                            QHBoxLayout *horizontalLayout_3;
                            QHBoxLayout *horizontalLayout_4;
                            QVBoxLayout *verticalLayout;
                            QSpacerItem *horinzontalspacer;

                            void setupUi(QMainWindow *DemoApp)
                            {
                                if (DemoApp->objectName().isEmpty())
                                    DemoApp->setObjectName(QString::fromUtf8("DemoApp"));
                                DemoApp->resize(461, 165);
                                centralWidget = new QWidget(DemoApp);
                                centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
                                pushButton = new QPushButton(centralWidget);
                                pushButton->setObjectName(QString::fromUtf8("pushButton"));
                                pushButton->setEnabled(false);
                                pushButton->setGeometry(QRect(20, 50, 91, 21));
                                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);
                            

                            /*
                            with the following commented lines, program crashes
                            */
                            // horizontalLayout->addWidget(deviceStatus);
                            // horizontalLayout->addWidget(label_3);

                            // horizontalLayout_2->addWidget(pushButton);
                            // horizontalLayout_2->addWidget(horinzontalspacer);

                            // verticalLayout->addLayout(horizontalLayout);
                            // verticalLayout->addLayout(horizontalLayout_2);

                                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", "PushOn", 0));
                                label_3->setText(QApplication::translate("DemoApp", "Status", 0));
                                deviceStatus->setText(QApplication::translate("DemoApp", "Device Not Detected", 0));
                            } // retranslateUi
                            

                            };

                            namespace Ui {
                            class DemoApp: public Ui_DemoApp {};
                            } // namespace Ui

                            QT_END_NAMESPACE

                            #endif // UI_DEMOAPP_H
                            @

                            @
                            DemoApp::DemoApp(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::DemoApp)
                            {
                            ui->setupUi(this);
                            extendGui(this);
                            }
                            @

                            @
                            void DemoApp::extendGui(QWidget *parent)
                            {

                            QLabel *label_Status = new QLabel("Staus");
                            label_Status->setObjectName(QString::fromUtf8("Stat3"));
                            label_Status->setEnabled(true);
                            label_Status->setGeometry(QRect(410, 10, 41, 21));
                            label_Status->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
                            QLineEdit *deviceStatus1 = new QLineEdit("Device Status");
                            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_Status);
                            
                            
                            QVBoxLayout *mainLayout = new QVBoxLayout;
                                // mainLayout->addWidget(statusLayout);
                                //mainLayout->addLayout(titleLayout);
                            
                                mainLayout->addLayout(statusLayout);  
                                    QWidget *w = new QWidget(this);
                                    w->setLayout(mainLayout); 
                                    w->setWindowTitle("layouts");
                            
                                    w->show();
                            

                            // verticallayout->addLayout(w);
                            //ui->centralWidget->layout()->addWidget(mainLayout);

                            }

                            @

                            1 Reply Last reply
                            0
                            • J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14
                              /*
                              with the following commented lines, program crashes
                              */
                              // horizontalLayout->addWidget(deviceStatus);
                              // horizontalLayout->addWidget(label_3);
                              
                              // horizontalLayout_2->addWidget(pushButton);
                              // horizontalLayout_2->addWidget(horinzontalspacer);
                              
                              // verticalLayout->addLayout(horizontalLayout);
                              // verticalLayout->addLayout(horizontalLayout_2);
                              

                              You define horizontalLayout, horizontalLayout_2 and verticalLayout as pointers, but they don't point to an object. THis causes the crash.

                              try

                              horizontalLayout = new QHBoxLayout();
                              

                              and you can add widgets to the Layout. in your special case here:

                              centralWidget = new QWidget(DemoApp);
                                  centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
                                  pushButton = new QPushButton();
                                  ...
                                  label_3 = new QLabel();
                                  ...
                                  deviceStatus = new QLineEdit(centralWidget);
                                  ...
                              
                              
                                  QHBoxLayout *horizontalLayout = new QHBoxLayout();
                                  horizontalLayout->addWidget(pushButton);
                                  horizontalLayout->addWidget(label_3);
                                  horizontalLayout->addWidget(deviceStatus);
                                  centralWidget->setLayout(horizontalLayout);
                              

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              O 1 Reply Last reply
                              1
                              • J.HilkJ J.Hilk
                                /*
                                with the following commented lines, program crashes
                                */
                                // horizontalLayout->addWidget(deviceStatus);
                                // horizontalLayout->addWidget(label_3);
                                
                                // horizontalLayout_2->addWidget(pushButton);
                                // horizontalLayout_2->addWidget(horinzontalspacer);
                                
                                // verticalLayout->addLayout(horizontalLayout);
                                // verticalLayout->addLayout(horizontalLayout_2);
                                

                                You define horizontalLayout, horizontalLayout_2 and verticalLayout as pointers, but they don't point to an object. THis causes the crash.

                                try

                                horizontalLayout = new QHBoxLayout();
                                

                                and you can add widgets to the Layout. in your special case here:

                                centralWidget = new QWidget(DemoApp);
                                    centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
                                    pushButton = new QPushButton();
                                    ...
                                    label_3 = new QLabel();
                                    ...
                                    deviceStatus = new QLineEdit(centralWidget);
                                    ...
                                
                                
                                    QHBoxLayout *horizontalLayout = new QHBoxLayout();
                                    horizontalLayout->addWidget(pushButton);
                                    horizontalLayout->addWidget(label_3);
                                    horizontalLayout->addWidget(deviceStatus);
                                    centralWidget->setLayout(horizontalLayout);
                                
                                O Offline
                                O Offline
                                o6a6r9v1p
                                wrote on last edited by
                                #15

                                @J.Hilk
                                I rewrote the entire program avoiding Qtcreator, to extend that later on.
                                the program is:

                                @
                                //main.cpp
                                #include <QApplication>
                                #include <QPushButton>
                                #include <QHBoxLayout>
                                #include <QVBoxLayout>
                                #include <QWidget>
                                #include "demoapp.h"

                                int main (int argc, char* argv[])
                                {
                                QApplication app(argc, argv);

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

                                }
                                @

                                @
                                //demoapp.h
                                #ifndef DEMOAPP_H
                                #define DEMOAPP_H

                                #include <QMainWindow>

                                #include <QProgressBar>
                                #include <QLabel>
                                #include <QLineEdit>
                                #include <QHBoxLayout>
                                #include <QPushButton>

                                class QLineEdit;

                                class DemoApp : public QMainWindow
                                {
                                Q_OBJECT

                                public:
                                explicit DemoApp(QWidget *parent = 0);
                                ~DemoApp();
                                public:
                                QWidget *centralWidget;
                                QProgressBar *progressBar;
                                QPushButton *pushButton;
                                QLabel *pushbuttonStatus;
                                QLabel *label_2;
                                QLabel *label_3;
                                QLineEdit *deviceStatus;
                                QMenuBar *menuBar;
                                QStatusBar *statusBar;
                                QHBoxLayout *horizontalLayout;
                                QHBoxLayout *horizontalLayout_2;
                                QHBoxLayout *horizontalLayout_3;
                                QHBoxLayout *horizontalLayout_4;
                                QVBoxLayout *verticalLayout;
                                QSpacerItem *horinzontalspacer;

                                private:
                                void setupUi();
                                public slots:
                                void update_gui(bool Connected, bool isPressed, int Value);

                                signals:
                                void pushButton_pressed();

                                private slots:
                                void on_pushButton_clicked();
                                };

                                #endif // DEMOAPP_H
                                @

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

                                #include <QLabel>
                                #include <QLineEdit>
                                #include <QHBoxLayout>
                                #include <QVBoxLayout>
                                #include <QWidget>

                                DemoApp::DemoApp(QWidget *parent) :
                                QMainWindow(parent)
                                {
                                progressBar = new QProgressBar(this);
                                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(this);
                                pushButton->setObjectName(QString::fromUtf8("pushButton"));
                                pushButton->setEnabled(false);
                                pushButton->setGeometry(QRect(20, 50, 91, 21));
                                pushbuttonStatus = new QLabel(this);
                                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(this);
                                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(this);
                                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);

                                QHBoxLayout *horizontalLayout = new QHBoxLayout();
                                horizontalLayout->addWidget(deviceStatus);
                                horizontalLayout->addWidget(label_3);
                                
                                QHBoxLayout *horizontalLayout1 = new QHBoxLayout();
                                horizontalLayout1->addWidget(pushButton);
                                horizontalLayout1->addWidget(pushbuttonStatus);
                                
                                QVBoxLayout *verticalLayout = new QVBoxLayout();
                                
                                verticalLayout->addLayout(horizontalLayout);
                                verticalLayout->addLayout(horizontalLayout1);
                                verticalLayout->addWidget(label_2);
                                verticalLayout->addWidget(progressBar);
                                setLayout(verticalLayout);
                                setWindowTitle(tr("Device Monitor"));
                                

                                //setupUi();
                                }

                                DemoApp::~DemoApp()
                                {
                                }

                                void DemoApp::setupUi()
                                {
                                ;
                                }

                                void DemoApp::on_pushButton_clicked()
                                {
                                emit pushButton_pressed();
                                }

                                void DemoApp::update_gui(bool Connected, bool isPressed, int Value)
                                {
                                if(Connected)
                                {
                                label_2->setEnabled(true);
                                pushButton->setEnabled(true);
                                pushbuttonStatus->setEnabled(true);
                                progressBar->setEnabled(true);

                                    deviceStatus->setText("Device Connected");
                                
                                    if(isPressed)
                                        pushbuttonStatus->setText("Pushbutton : Pressed");
                                    else
                                        pushbuttonStatus->setText("Pushbutton : Not Pressed");
                                
                                    progressBar->setValue(Value);
                                }
                                else
                                {
                                    label_2->setEnabled(false);
                                    pushButton->setEnabled(false);
                                    pushbuttonStatus->setEnabled(false);
                                    progressBar->setEnabled(false);
                                    deviceStatus->setText("Device Not connected");
                                    pushbuttonStatus->setText("Pushbutton : Unknown");
                                    progressBar->setValue(0);
                                }
                                

                                }
                                @

                                when it is run it gives following message:
                                QObject::setParent: Cannot set parent, new parent is in a different thread
                                The program has unexpectedly finished.

                                I did not get where exactly problem is.

                                O 1 Reply Last reply
                                0
                                • O o6a6r9v1p

                                  @J.Hilk
                                  I rewrote the entire program avoiding Qtcreator, to extend that later on.
                                  the program is:

                                  @
                                  //main.cpp
                                  #include <QApplication>
                                  #include <QPushButton>
                                  #include <QHBoxLayout>
                                  #include <QVBoxLayout>
                                  #include <QWidget>
                                  #include "demoapp.h"

                                  int main (int argc, char* argv[])
                                  {
                                  QApplication app(argc, argv);

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

                                  }
                                  @

                                  @
                                  //demoapp.h
                                  #ifndef DEMOAPP_H
                                  #define DEMOAPP_H

                                  #include <QMainWindow>

                                  #include <QProgressBar>
                                  #include <QLabel>
                                  #include <QLineEdit>
                                  #include <QHBoxLayout>
                                  #include <QPushButton>

                                  class QLineEdit;

                                  class DemoApp : public QMainWindow
                                  {
                                  Q_OBJECT

                                  public:
                                  explicit DemoApp(QWidget *parent = 0);
                                  ~DemoApp();
                                  public:
                                  QWidget *centralWidget;
                                  QProgressBar *progressBar;
                                  QPushButton *pushButton;
                                  QLabel *pushbuttonStatus;
                                  QLabel *label_2;
                                  QLabel *label_3;
                                  QLineEdit *deviceStatus;
                                  QMenuBar *menuBar;
                                  QStatusBar *statusBar;
                                  QHBoxLayout *horizontalLayout;
                                  QHBoxLayout *horizontalLayout_2;
                                  QHBoxLayout *horizontalLayout_3;
                                  QHBoxLayout *horizontalLayout_4;
                                  QVBoxLayout *verticalLayout;
                                  QSpacerItem *horinzontalspacer;

                                  private:
                                  void setupUi();
                                  public slots:
                                  void update_gui(bool Connected, bool isPressed, int Value);

                                  signals:
                                  void pushButton_pressed();

                                  private slots:
                                  void on_pushButton_clicked();
                                  };

                                  #endif // DEMOAPP_H
                                  @

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

                                  #include <QLabel>
                                  #include <QLineEdit>
                                  #include <QHBoxLayout>
                                  #include <QVBoxLayout>
                                  #include <QWidget>

                                  DemoApp::DemoApp(QWidget *parent) :
                                  QMainWindow(parent)
                                  {
                                  progressBar = new QProgressBar(this);
                                  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(this);
                                  pushButton->setObjectName(QString::fromUtf8("pushButton"));
                                  pushButton->setEnabled(false);
                                  pushButton->setGeometry(QRect(20, 50, 91, 21));
                                  pushbuttonStatus = new QLabel(this);
                                  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(this);
                                  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(this);
                                  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);

                                  QHBoxLayout *horizontalLayout = new QHBoxLayout();
                                  horizontalLayout->addWidget(deviceStatus);
                                  horizontalLayout->addWidget(label_3);
                                  
                                  QHBoxLayout *horizontalLayout1 = new QHBoxLayout();
                                  horizontalLayout1->addWidget(pushButton);
                                  horizontalLayout1->addWidget(pushbuttonStatus);
                                  
                                  QVBoxLayout *verticalLayout = new QVBoxLayout();
                                  
                                  verticalLayout->addLayout(horizontalLayout);
                                  verticalLayout->addLayout(horizontalLayout1);
                                  verticalLayout->addWidget(label_2);
                                  verticalLayout->addWidget(progressBar);
                                  setLayout(verticalLayout);
                                  setWindowTitle(tr("Device Monitor"));
                                  

                                  //setupUi();
                                  }

                                  DemoApp::~DemoApp()
                                  {
                                  }

                                  void DemoApp::setupUi()
                                  {
                                  ;
                                  }

                                  void DemoApp::on_pushButton_clicked()
                                  {
                                  emit pushButton_pressed();
                                  }

                                  void DemoApp::update_gui(bool Connected, bool isPressed, int Value)
                                  {
                                  if(Connected)
                                  {
                                  label_2->setEnabled(true);
                                  pushButton->setEnabled(true);
                                  pushbuttonStatus->setEnabled(true);
                                  progressBar->setEnabled(true);

                                      deviceStatus->setText("Device Connected");
                                  
                                      if(isPressed)
                                          pushbuttonStatus->setText("Pushbutton : Pressed");
                                      else
                                          pushbuttonStatus->setText("Pushbutton : Not Pressed");
                                  
                                      progressBar->setValue(Value);
                                  }
                                  else
                                  {
                                      label_2->setEnabled(false);
                                      pushButton->setEnabled(false);
                                      pushbuttonStatus->setEnabled(false);
                                      progressBar->setEnabled(false);
                                      deviceStatus->setText("Device Not connected");
                                      pushbuttonStatus->setText("Pushbutton : Unknown");
                                      progressBar->setValue(0);
                                  }
                                  

                                  }
                                  @

                                  when it is run it gives following message:
                                  QObject::setParent: Cannot set parent, new parent is in a different thread
                                  The program has unexpectedly finished.

                                  I did not get where exactly problem is.

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

                                  @o6a6r9v1p
                                  I have followed "Calculator" example code from Qt examples, while writing above application.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • O o6a6r9v1p

                                    @o6a6r9v1p
                                    I have followed "Calculator" example code from Qt examples, while writing above application.

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

                                    @o6a6r9v1p You're doing something wrong. In your class you declare variables for the layouts, but then in the constructor you define local variables with same names:

                                    QHBoxLayout *horizontalLayout = new QHBoxLayout();
                                    

                                    Are you aware that this horizontalLayout is not the same you declared in your class? If you later try to access it your app will crash. Just do it like this in your constructor:

                                    horizontalLayout = new QHBoxLayout();
                                    

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

                                    O 1 Reply Last reply
                                    2
                                    • jsulmJ jsulm

                                      @o6a6r9v1p You're doing something wrong. In your class you declare variables for the layouts, but then in the constructor you define local variables with same names:

                                      QHBoxLayout *horizontalLayout = new QHBoxLayout();
                                      

                                      Are you aware that this horizontalLayout is not the same you declared in your class? If you later try to access it your app will crash. Just do it like this in your constructor:

                                      horizontalLayout = new QHBoxLayout();
                                      
                                      O Offline
                                      O Offline
                                      o6a6r9v1p
                                      wrote on last edited by
                                      #18

                                      @jsulm
                                      Thank you very much. Now I corrected the program , it is running.
                                      Thanks to all who have helped me.

                                      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