Solved Added widgets manually to Qt generated code, shown in 2nd window
-
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.- widgets added in extendGui() are being displayed in another window, "label_Status" is missing.
- how to make Layout to automatically adjust size.
what is the mistake in the program.
[edit:koahnig] Code tags corrected
-
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. -
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.
-
@Venkatesh-V
Hi,
How to do this(setting to parent) -
@o6a6r9v1p You did not add w widget to any layout of your DemoApp.
-
QWidget *w = new QWidget(this);
but you still have to add it to a layout in your DemoApp.
-
@o6a6r9v1p
Qwidget *w = new QWidget(this); -
"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.
-
@jsulm said in Added widgets manually to Qt generated code, shown in 2nd window:
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.
-
@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).
-
@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.
-
@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.
-
@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 UiQT_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);}
@
-
/* 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);
-
@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_OBJECTpublic:
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.
-
@o6a6r9v1p
I have followed "Calculator" example code from Qt examples, while writing above application. -
@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();
-
@jsulm
Thank you very much. Now I corrected the program , it is running.
Thanks to all who have helped me.