PDA

View Full Version : How to develop a multi-gui application "alternative for the stacked widget "



sliverTwist
13th March 2013, 12:34
Hello i am a beginner my objective is to develop a multi interfaces application where each interface "widget" had it's own .cpp and .h files .
my second question is how to link them up
i want an application which is similar in the structure and the classes to an android application where each interface has it's specifics files :confused: :confused:

wysota
13th March 2013, 13:15
Use QStackedWidget.

sliverTwist
13th March 2013, 13:20
i know already that but the stackedwidget doesn't allow me to seperate the code of each page "widget" in a separated .cpp and .h file

wysota
13th March 2013, 13:23
Of course it allows you to implement the code of each page in a separate set of files.

sliverTwist
13th March 2013, 13:25
how is that ??? I have already tried it but when implement a new page in the QstackedWidget i don't get a new set of files for the new page

wysota
13th March 2013, 13:47
QStackedWidget::addWidget() -- this accepts pointer to a widget. Any widget. Implemented in any file.

sliverTwist
13th March 2013, 15:29
Sorry for asking again but i have a problem using this method
i have created a new QtDesignerForm class called Widget2
do i call this widget like this ??
ui->stackedWidget->addWidget(Widget2 );

d_stranz
13th March 2013, 15:48
do i call this widget like this ??
ui->stackedWidget->addWidget(Widget2 );

No. You do this:



Widget2 * newWidget2 = new Widget2;
ui->stackedWidget->addWidget( newWidget2 );


As Wysota said, the code for "Widget2" can be implemented *anywhere*. It can even be implemented in a library (such as Qt itself) and all you need to create an instance of "Widget2" is the header file that defines the class and the library containing the compiled code.

If you are trying to create new QWidget classes of your own and you are not ending up with new .h and .cpp files in which to implement the code for those classes, then you are not using your application development tool correctly or you have it configured wrong.

sliverTwist
14th March 2013, 07:27
Well at first i created in my project a new Qt Designer Form Class which is a widget called widget2 that created an new .ui/.cpp/.h files then i wrote this code in my mainwindow wich contain a QStackedWidget


ui->stackedWidget->setCurrentIndex(0);
Widget2 * newWidget2 = new Widget2;
ui->stackedWidget->addWidget( newWidget2 );
but i doesn't do the job :mad: :mad:

wysota
14th March 2013, 09:43
In what way "it doesn't do the job"?

sliverTwist
14th March 2013, 09:53
In what way "it doesn't do the job"?
Well simply i don't get that interface when i start the application i get just an empty interface which is the interface for the QStackedwidget

Added after 5 minutes:

this is the code of my mainWindow


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "widget2.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
Widget2 *newWidget2 = new Widget2;
ui->stackedWidget->addWidget(newWidget2);

}

MainWindow::~MainWindow()
{
delete ui;

}

wysota
14th March 2013, 10:01
I'm assuming your stack widget has two pages that are added to it by default by Designer so if you set current index to 0, you see one of those two blank pages. Either delete those two pages or set current index to 2.

sliverTwist
14th March 2013, 10:11
Setting the index to 2 didn't do the job neither deleting because there's no way to delete the default 2 pages created by the Designer

Added after 5 minutes:

I am currently working on OpenSUSE 12 64Bit and my Qt is Qt Creator 2.5.0 Based on Qt 4.8.1 (64 bit)

wysota
14th March 2013, 10:39
Setting the index to 2 didn't do the job
A riddle for you: if a book has 10 pages and you try turning to page 123 then it won't work. But if a book has 123 pages then it will work.


neither deleting because there's no way to delete the default 2 pages created by the Designer
I don't have any problems deleting pages created by Designer.

anda_skoa
14th March 2013, 11:07
Alternatively fill the stack with empty widgets for each page in designer and then promote each of those to the class that should be at the respective page.

So for example if you want Widget2 to be the first page, promote the widget in page 1 to type Widget2.

Cheers,
_

ChrisW67
14th March 2013, 22:05
neither deleting because there's no way to delete the default 2 pages created by the Designer
In the Object browser pane:

Right-click on the stacked widget
Select Page 1 of 2 from the context menu
Select Delete
Rinse and repeat


Or, because TMTOWTDI, you can do it in code:


QWidget *widget = ui->stackedWidget->index(0);
ui->stackedWidget->removeWidget(widget);
widget->deleteLater();