Allow Many QWidgets to be Shown into One QWidget ?
I've created Many (.UI) , made a (Header File + .cpp) to each .UI in order to implement their own slots , & Linked them by making a widget to be shown using :
Code:
// CustomSlot2 implements the slots inside the 2nd .UI
CustomSlot2* widget = new CustomSlot2();
widget->setAttribute(Qt::WA_DeleteOnClose);
widget->show();
CustomSlot2 Constructor's Code is :
Code:
{
formTwo.setupUi(this);
}
So far so Good . However , by making this approach there's a flicker when you Open & Hide/Close Windows .
I've thought in an alternative way ;that I'll Drag & Drop a QWidget into my Main Window using QT Design.
The Problem : How Could I open my .UIs into the QWidget that I've added @ MainWindow .
The MainWindow's header file generated by "make" :
Code:
class Ui_MainForm
{
public:
//........................ etc
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
The ui must be creating classes for you, isnt it ?
well add those classes/windows into ur main window as u would add a widget...
hope i understand what you meant
Re: Allow Many QWidgets to be Shown into One QWidget ?
Quote:
well add those classes/windows into ur main window as u would add a widget...
How could I make all the .UIs show in the same Widget @ Main Window ?
Quote:
hope i understand what you meant
Actually , It's My mistake that I didn't clarify my Problem well .
So , Let me clarify it again ;
Now , I'm @ QT Design then I create a normal widget ,add ctrls into it.
I make the previous step 4 times . So now I've 4 .UIs that have there own ctrls into it .
To Each UI, I create its (.h + .cpp ) to implement my own slots .
Well now I open ,using QT Design ,the 1st .ui I 've made & Drag & Drop Widget into it , which is located into Container Section .
The Problem : is how to make other .UIs open into the widget I created @ the 1st .ui I 've made .
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
You can add a plain QWidget on the form and promote it as a custom widget (which loads another ui).
Re: Allow Many QWidgets to be Shown into One QWidget ?
Thanks aamer4yu.
Perfect jpn :) .
It works now after using Promoting .
@ the MainWindow I've added a plain QWidget into it .
I've noticed that its header file generated by "make" creates instance of CustomSlot (which implements the slots of the form appears into the plain widget @ MainWindow form .
The Problem :now I want to replace the .UI that opened into the plain widget @ MainWindow with another .UI @ SAme plain Widget
Header File's code generated of MainWindow that Contains the plain widget :
Code:
class Ui_MainForm
{
public:
CustomSlot *MiniWidget; // MiniWidget = The Plain widget
{
if (MainForm->objectName().isEmpty())
MainForm
->setObjectName
(QString::fromUtf8("MainForm"));
MainForm->resize(530, 402);
MiniWidget = new CustomSlot(MainForm);
MiniWidget
->setObjectName
(QString::fromUtf8("MiniWidget"));
MiniWidget
->setGeometry
(QRect(39,
29,
451,
301));
retranslateUi(MainForm);
} // setupUi
void retranslateUi
(QWidget *MainForm
) {
Q_UNUSED(MainForm);
} // retranslateUi
};
namespace Ui {
class MainForm: public Ui_MainForm {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
Main.cpp 's code :
Code:
#include <QApplication>
#include "ui_MainWindow.h"
#include "MainApp.cpp"
int main(int argc, char *argv[])
{
MainApp widget;
widget.show();
return app.exec();
}
The MainApp's code that implements slots of the MainWindow :
Code:
{
ui.setupUi(this);
}
Thanks
Re: Allow Many QWidgets to be Shown into One QWidget ?
What do u mean replace ? If you mean to show another widget in same area, you can have a look at QStackedWidget. It allows you to show one widget at a time in the same area :)
Re: Allow Many QWidgets to be Shown into One QWidget ?
So , do you mean that there 's NO Way to use the plain widget to solve my problem ?!!
Mmmm what about using many plain widgets @ the MainWindow & specify each one of them to a .ui ?!!
If I follow that way , how could I ctrl in showing & closing/hiding the plain widgets ?
Thanks
Re: Allow Many QWidgets to be Shown into One QWidget ?
Just place a stacked widget on your form as proposed. Then make each page of the stack contain a different custom widget (which all load their own ui files). Switching from one custom widget to another is only a matter of switching the current widget in the stack.
Re: Allow Many QWidgets to be Shown into One QWidget ?
Quote:
Originally Posted by
jpn
Just place a stacked widget on your form as proposed. Then make each page of the stack contain a different custom widget (which all load their own ui files). Switching from one custom widget to another is only a matter of switching the current widget in the stack.
Actually I put set of plain widgets over each other @ my MainWindow .
Then I promote each one to the proper .ui .
Then @ my main : I've created instance of QStackedWidget & put into it the plain widgets that I've just created .
Problems :
- Program doesn't run even an .exe is generated + Exception is appeared
- I was thinking that after creating my instance of StackedObject, I'll use it to ctrl which UI is going to be appeared , How could I achieve smth like that ?!!
My Main 's code :
Code:
#include <QApplication>
#include "ui_MainWindow.h"
#include "MainApp.cpp"
#include <QStackedWidget>
int main(int argc, char *argv[])
{
Ui_MainForm ui;
StackedWidget->addWidget(ui.MiniWidget);
StackedWidget->addWidget(ui.MiniWidget2);
MainApp Widget;
Widget.show();
return app.exec();
}
-------------------------------------------------------------------------
MainWindow's generated code that contains the plain widgetS :
Code:
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QWidget>
#include <customslot2.h>
#include "customslot.h"
QT_BEGIN_NAMESPACE
class Ui_MainForm
{
public:
// These are the Plain Widgets I created
CustomSlot *MiniWidget;
CustomSlot2 *MiniWidget2;
{
if (MainForm->objectName().isEmpty())
MainForm
->setObjectName
(QString::fromUtf8("MainForm"));
MainForm->resize(530, 402);
MiniWidget = new CustomSlot(MainForm);
MiniWidget
->setObjectName
(QString::fromUtf8("MiniWidget"));
MiniWidget
->setGeometry
(QRect(39,
29,
451,
301));
MiniWidget2 = new CustomSlot2(MiniWidget);
MiniWidget2
->setObjectName
(QString::fromUtf8("MiniWidget2"));
MiniWidget2
->setGeometry
(QRect(0,
110,
461,
301));
retranslateUi(MainForm);
} // setupUi
void retranslateUi
(QWidget *MainForm
) {
Q_UNUSED(MainForm);
} // retranslateUi
};
namespace Ui {
class MainForm: public Ui_MainForm {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
Thanks
Re: Allow Many QWidgets to be Shown into One QWidget ?
I've tried many times to use QStackedWidget . However , I'm facing a few difficulties with it .
Problem : How Could I use it into my app ,mentioned b4 ?!!
Re: Allow Many QWidgets to be Shown into One QWidget ?
I've made a little progress .
I've just noticed that there's @ QT Design smth called "stacked Widget" .
I 've promoted to the Stacked Widget one of my .UI .
Problem :
- I got an error : 'class CustomSlot' has no member named 'addWidget' ?!!
- I 've noticed that I can add any number of pages @ Stacked Widget , so How Can I promote different .UI , to each page I 've ?
Header File 's code that has the custom Widget
Code:
#ifndef UI_FORM2_H
#define UI_FORM2_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGroupBox>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QRadioButton>
#include <QtGui/QTableView>
#include <QtGui/QWidget>
#include <customslot.h>
QT_BEGIN_NAMESPACE
class Ui_FormTWo
{
public:
CustomSlot *stackedWidget;
{
stackedWidget = new CustomSlot(FormTwo);
stackedWidget
->setObjectName
(QString::fromUtf8("stackedWidget"));
stackedWidget
->setGeometry
(QRect(540,
230,
171,
131));
Form1
->setObjectName
(QString::fromUtf8("Form1"));
Form1
->setGeometry
(QRect(0,
0,
171,
131));
stackedWidget->addWidget(Form1);
Form2
->setObjectName
(QString::fromUtf8("Form2"));
Form2
->setGeometry
(QRect(0,
0,
171,
131));
stackedWidget->addWidget(Form2);
retranslateUi(FormTwo);
stackedWidget->setCurrentIndex(0);
} // setupUi
void retranslateUi
(QWidget *FormTanya
) {
// ...........................etc
} // retranslateUi
};
namespace Ui {
class FormTwo: public Ui_FormTwo {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_FORM2_H
---------------------------------------------------------------------------------
My CustomSlot 's code :
Code:
#include"CustomSlot.h"
#include "ui_Test.h"
#include "ui_Form2.h"
#include"CustomSlot2.cpp"
{
ui.setupUi(this);
}
// ......................etc
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
Please use proper language instead of sms slang. And please read QStackedWidget docs, by the way :) Promoting widgets is not the same as placing one widget in another and I think you are mixing the two.
Re: Allow Many QWidgets to be Shown into One QWidget ?
Quote:
Originally Posted by
wysota
Promoting widgets is not the same as placing one widget in another and I think you are mixing the two.
YES I WAS .
I'd already read the QStackedWidget from the docs b4 many times . However after what you 'd just told me , things become more clear now .
I've made a simple app that just contains : Button + Custom Widget (Dragged & Dropped using QT Desgin)
I want each time the user clicks the button , different .UIs to be shown .
Problem :
-Every time I run the app , Exception is appeared
-I just added these line at my main :
Code:
Ui_FormTwo FormTwoObj; // UI that contains my Stacked Widget
CustomSlot *CustomSlotObj; // The class that implements slots of UI I want to display to the user
FormTwo.stackedWidget->addWidget(CustomSlotObj);
- I Still don't understand well the QStackedWidget . What I Understand that QStackedWidget is a stack that you can push & pop like the normal stack with the capability of controlling with the index of Removing / Wanted objects in the stack .
So now How could I make an object at stack to be appeared to the user ?
Quote:
Originally Posted by
wysota
Please use proper language instead of sms slang.
Thanks for notifying me :)
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
Treat the stacked widget as a book where you can add or remove pages (but this is not the important part here) and where you can open the book at any page at any moment and view that page (that's the important part) and only that page.
Re: Allow Many QWidgets to be Shown into One QWidget ?
That's Wonderful .
1/2 of my problem 's been solved .
I've just noticed that when I drag & drop QStackedWidget using QT Design , Pages (instance of Widgets ) are created & added to the stacked widget .
I can add any number of pages I want & add the controls into it . I can control which widget to be displayed .
It works so great .
The second half of my problem :
- Whenever I try to declare instace of my .UI & add it to the QStackedWidget , Exception is appeared .
I've tried :
addWidget(WidgetObj);
widget(index );
insertWidget (index , WidgetObj);
CustomSlot's code that implements slots of the .UI & also has the QStackedWidget :
Code:
#include"CustomSlot2.h"
#include "ui_Form2.h"
#include "CustomSlot.h"
{
formTwo.setupUi(this);
//CustomSlot is the class that implement the slots of the .UI that I want to add at QStackedWidget
CustomSlot *CustomSlotObj ;
//formTwo.stackedWidget->addWidget(CustomSlotObj);
// I've already just 2 pages at QStackedWidget , The third on is going to be in index 2
formTwo.stackedWidget->insertWidget(2,CustomSlotObj);
WidgetObj= formTwo.stackedWidget->widget (2) ;
WidgetObj->show();
}
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
"CustomSlot *CustomSlotObj;" produces nothing but a dangling pointer which points to some random memory garbage. You forgot to actually instantiate something with C++ operator new.
Re: Allow Many QWidgets to be Shown into One QWidget ?
Perfect , it works now .
However I still have simple issues :
1- When I add my .UI at the QStacked Widget , it successfully appears & I can just control it using Keyboard (i.e. the UI that I've added at QStacked Widget contains QLineEdit + Button I can't press the button using the mouse I should use my "Tab" in order to go to that control then press "Space" ) ?!!
2- According to what I've have understood that only one widget at QStackedWidget can be displayed at once to the user .
I just have one QWidget that is put by default at QStackedWidget , when I drag & drop the stack using QT Design . Then at this page I've added a button . Then using the code I 've added my .UI at the stack then show it .
What 's weird : that both QWidgets that have the button + the other Widget that I 've add by code are displayed ?
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
Sounds like you created the form as a child of the stacked widget but you didn't actually add the widget to the stack widget. That's at least one reason why it wouldn't be managed by the stacked widget.
Re: Allow Many QWidgets to be Shown into One QWidget ?
How is that ?!!
Part of Header files 's code generated to the form that contains the QStackedWidget :
Code:
#include <QtGui/QStackedWidget>
#include <QtGui/QWidget>
class Ui_FormTwo
{
public:
{
stackedWidget
->setObjectName
(QString::fromUtf8("stackedWidget"));
stackedWidget
->setGeometry
(QRect(460,
130,
551,
371));
page
->setObjectName
(QString::fromUtf8("page"));
page
->setGeometry
(QRect(0,
0,
551,
371));
stackedWidget->addWidget(page);
retranslateUi(FormTwo);
stackedWidget->setCurrentIndex(0);
// ......................etc
} // setupUi
---------------------------------------------------------------
Temporarily , I've added the code that add my .UI to the stack at the constructor of the form that has the stack object :
Code:
{
formTwo.setupUi(this);
CustomSlot *CustomSlotObj=new CustomSlot() ;
formTwo.stackedWidget->insertWidget(1,CustomSlotObj);
WidgetObj= formTwo.stackedWidget->widget (1) ;
WidgetObj->show();
}
Thanks .
Re: Allow Many QWidgets to be Shown into One QWidget ?
Aha, now I realized what's wrong.
Quote:
Originally Posted by
Fatla
Code:
WidgetObj= formTwo.stackedWidget->widget (1) ;
WidgetObj->show();
This is not the way you use QStackedWidget. You are supposed to use QStackedWidget::setCurrentIndex() or setCurrentWidget().