Solved namespace doubts in program
-
@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? -
@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! -
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.cppI am getting the error : "modifyGui" was not declared in this scope
where shall I put this function? -
@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 -
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. -
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 mainLayoutAlternatively , 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? -
@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.