Is Stacked Widget the only option
-
Hi all,
I am trying to build a project in which there are multiple screens. Functionality I am trying to put is to click a button and go back to next screen and then from any screen I can come back to the main menu...
Now I am implementing this via. signal slot mechanism. The main priority is that every time I open any screen the previous should be hidden. Since for each screen I have different include and headers and form files, I am trying to create an instance of the class and then using it in signal slot mechanism...My code illustrates further....Here there are two screens (DMMMenu...the main menu and the Actual screen....I am able to go from main menu to Actual screen but cant come back to it.
Do it is necessary to use Stackedwidget ?DMMMenu.h:
@
#ifndef DMM_H
#define DMM_H
#include <QtGui>
#include <QWidget>
#include "../../DMM/Include/Actual.h"
#include "../../DMM/Include/DMM.h"
class Actual;
namespace Ui {class DMMMenu;
}
namespace DMM {class DMMMenu: public QWidget
{
Q_OBJECTpublic:
explicit DMMMenu(QWidget *parent = 0);
~DMMMenu();public slots:
void OnVoltageClicked();
private:
DMM::DMMMenu *MainWindow;
Ui::DMMMenu *ui;
Actual *Ac;};
}
#endif // DMM_H
@
DMM.cpp
@
include "../../Include/DMM.h"
#include "../../DMM/Include/Actual.h"
include "ui_DMMMenu.h"
#include <QDateTime>
namespace DMM
{DMMMenu::DMMMenu(QWidget *parent) :
QWidget(parent),ui(new ::Ui::DMMMenu)
{
ui->setupUi(this); Ac=new Actual; Ac->hide(); connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(OnVoltageClicked()));
//get current date
QDate date = QDate::currentDate();
QString dateString = date.toString();
ui->Label_Date->setText(dateString);// get current time
QTime time = QTime::currentTime();
QString timeString = time.toString();
ui->Label_Time->setText(timeString);}
void DMMMenu::OnVoltageClicked()
{
Ac->show();
this->hide();}
DMMMenu::~DMMMenu()
{}
}
@
But when I am trying to do the same for Actual that is
Actual.cpp@
#include "../../DMM/Include/Actual.h"
#include "../../DMM/Include/DMM.h"
#include "ui_Actual.h"Actual::Actual(QWidget *parent) :
QWidget(parent),
ui(new Ui::Actual)
{ui->setupUi(this);
connect(ui->pushButton, SIGNAL clicked()),this, SLOT on_pushButton_clicked());
home=new DMMMenu; home->hide();
}
Actual::~Actual()
{
delete ui;
}void Actual::on_pushButton_clicked()
{
home->show();
}@
I get errors :
/home/pragati/MultiFuncTester/Components/DMM/Build/../Source/Actual.cpp:18: error: invalid use of incomplete type 'struct DMMMenu'/home/pragati/MultiFuncTester/Components/DMM/Build/../../DMM/Include/Actual.h:7: error: forward declaration of 'struct DMMMenu'
-
You should use QStackedWidget and save yourself a bunch of effort.
-
:).....but Chris.....Again I have to start from the scratch to use stackedwidget or I can somehow convert my ui into stackedwidget
-
Have you tried including DMMMenu.h in you Actual.cpp?
@
#include "../../DMM/Include/Actual.h
#include "../../DMM/Include/DMM.h // Is this the header file that contains DMMMenu.h?
#include "../../DMM/Include/DMMMenu.h
#include "ui_Actual.h"
@
This is a cyclic dependency wherein your DMMMenu needs to know Actual and your Actual needs to know DMMMenu.Well, your requirement rightfully fits with/requires "QStackedWidget":http://doc-snapshot.qt-project.org/4.8/qstackedwidget.html - hence would suggest you to go thru it and use it - it reduces lot of complexity and pretty straight forward and simple to use it as well.
-
yes the DMM.h is the one which contains dmmmenu definition.
Actually we have to use these screens everywhere frequently in different subdirs which are part of this project so...stackwidget will not create a problem?
-
No. You can use as many instances of widgets as you like in as many places as you like. Some of those instances you can place in a QStackedWidget,
-
Will stacked widget create any problem in memory allocation or take more space ?
-
listen to people, QStackedWidget is your friend
-
@infoctopus.....:).......Yeah I want to....but some of my colleagues think that QStackedWidget will affect system's memory....So just to be assure and stay with QStackedWidget I need to be clear at this point of view....so guys help me out in deciding....please!!!!
-
My requirement is....I am going to have four different components in my project with lots of screens in it each and many will interact with each other....Its like a gui we have in our mobile...you can go back to main menu then switch to some other....the only difference is I am having different modes on which the device will work.....
So what you say what shall be the approach to achieve this kinda functionality
-
pragati, you either have your widgets (for "screens") in memory all the time (regardless of using QStackedWidget) OR you create/delete that widgets dynamically. I suggest to start with QStackedWidget and see if memory consumption is acceptable. If not, then create/delete objects when needed / not needed.
-
:)
I am already facing a problem in connecting widgets.....I am able to go next screen from main menu but from that screen I am not able to come back again to main menu. It is not allowing me to create an instance of the main menu's class in the screen from which I have to go back
:( -
you can post a project illustrating the issue so people could help you in their little time
-
Yeah I will do .....thanx anyways!!!