[Qt-interest] MainWindow and few QWidgets as a pages - is that right way?
franki
franki at franki.eu.org
Wed Nov 18 23:17:06 CET 2009
Tuesday 17 of November 2009 17:23:14 BOUCARD Olivier napisał(a):
> Hi,
>
> Actually the Qt's way to do this kind of display is to use QStackedWidget
> as QMainWindow central widget.
Hi,
Thank You for answer,
QStackedWidget looks like what I'm looking for. I've made small example, i've
put QStackedWidget inside Main Window template in designer, and some button's
inside pages, and switching from page to page through action in QMenu works
fine, but in my example QStackedWidget isn't really centralWidget of
QMainWindow, is it?
So in second attempt I've made in designer file (mainwindow.ui) using
MainWindow template
and second file (stacked.ui) where basic widget is QStackedWidget in witch my
interface should be really build.
Then I tried to initialize (setupUi) class with QStackedWidget after
initializing QMainWindow also through setupUi, and then made setCentralWidget
with widget from QStackedWidget
I belive that this is way it should be done?
But there is error during compilation:
stacked.cpp: In constructor 'StackedWidget::StackedWidget(QWidget*)':
stacked.cpp:7: error: no matching function for call
to 'Ui::StackedWidget::setupUi(StackedWidget* const)'
ui_stacked.h:43: note: candidates are: void
Ui_StackedWidget::setupUi(QStackedWidget*)
So I probably messed up with class names or so.
I've tried to change class and widget names, and some other things, but no
luck, could You give me a hint?
Best Regards
Marek
>
>
>
>
> ________________________________
> De : franki <franki at franki.eu.org>
> À : qt-interest at trolltech.com
> Envoyé le : Mar 17 Novembre 2009, 15 h 06 min 34 s
> Objet : [Qt-interest] MainWindow and few QWidgets as a pages - is that
> right way?
>
> Hello,
>
> I'm about to write application with one MainWindow, and few pages with
> different widgets inside, pages should be displayed inside the same window,
> after involving action from menu, or toolbar (much like clicking on website
> application)
> So is my approach correct, or should it be done in different way?
>
> These "pages", I would like to design in designer as a QWidgets, and when
> page "is requested" I would initialize class with page.setupUi, and use
> setCentralWidget(page) to display widget inside MainWindow.
> If there is already some page displayed and initialized inside MainWindow
> prevoiusly, should I simply destroy main object prior initialized with
> setupUi(), before I setCentralWidget with new object?
>
> Is that the way application should be build?
>
> I've made small test, but it's seems there is something wrong because when
> application starts, MainWindow is showing correctly, but centralwidget is
> empty (there aren't any widget's inside)
> files qtdesign3 - contain MainWindow class
> files test1 - contain QWidget that should be displayed as centralWidget
>
> Thank you in advance for your time.
> Marek
>
> code below
>
> main.cpp:
> #include "qtdesign3.h"
>
> int main(int argc, char *argv[])
> {
> QApplication app(argc, argv);
> MainWindow *window = new MainWindow;
> window->setWindowTitle("window test");
> window->show();
> return app.exec();
> }
>
> qtdesign3.h:
> #include "ui_mainwindow.h"
> #include <QLocale>
>
>
> class MainWindow: public QMainWindow
> {
> Q_OBJECT
> public:
> MainWindow();
> private:
> Ui::MainWindow ui;
> };
>
> ui_mainwindow.h:
> #ifndef UI_MAINWINDOW_H
> #define UI_MAINWINDOW_H
>
> #include <QtCore/QVariant>
> #include <QtGui/QAction>
> #include <QtGui/QApplication>
> #include <QtGui/QButtonGroup>
> #include <QtGui/QMainWindow>
> #include <QtGui/QMenu>
> #include <QtGui/QMenuBar>
> #include <QtGui/QStatusBar>
> #include <QtGui/QWidget>
>
> QT_BEGIN_NAMESPACE
>
> class Ui_MainWindow
> {
> public:
> QAction *actionTest;
> QAction *actionTest2;
> QWidget *centralwidget;
> QMenuBar *menubar;
> QMenu *menuTest;
> QMenu *menuTest2;
> QStatusBar *statusbar;
>
> void setupUi(QMainWindow *MainWindow)
> {
> if (MainWindow->objectName().isEmpty())
> MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
> MainWindow->resize(800, 600);
> actionTest = new QAction(MainWindow);
> actionTest->setObjectName(QString::fromUtf8("actionTest"));
> actionTest2 = new QAction(MainWindow);
> actionTest2->setObjectName(QString::fromUtf8("actionTest2"));
> centralwidget = new QWidget(MainWindow);
> centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
> MainWindow->setCentralWidget(centralwidget);
> menubar = new QMenuBar(MainWindow);
> menubar->setObjectName(QString::fromUtf8("menubar"));
> menubar->setGeometry(QRect(0, 0, 800, 31));
> menuTest = new QMenu(menubar);
> menuTest->setObjectName(QString::fromUtf8("menuTest"));
> menuTest2 = new QMenu(menubar);
> menuTest2->setObjectName(QString::fromUtf8("menuTest2"));
> MainWindow->setMenuBar(menubar);
> statusbar = new QStatusBar(MainWindow);
> statusbar->setObjectName(QString::fromUtf8("statusbar"));
> MainWindow->setStatusBar(statusbar);
>
> menubar->addAction(menuTest->menuAction());
> menubar->addAction(menuTest2->menuAction());
> menuTest->addAction(actionTest);
> menuTest2->addAction(actionTest2);
>
> retranslateUi(MainWindow);
>
> QMetaObject::connectSlotsByName(MainWindow);
> } // setupUi
>
> void retranslateUi(QMainWindow *MainWindow)
> {
>
> MainWindow->setWindowTitle(QApplication::translate("MainWindow",
> "MainWindow", 0, QApplication::UnicodeUTF8));
> actionTest->setText(QApplication::translate("MainWindow", "test", 0,
> QApplication::UnicodeUTF8));
> actionTest2->setText(QApplication::translate("MainWindow", "test2", 0,
> QApplication::UnicodeUTF8));
> menuTest->setTitle(QApplication::translate("MainWindow", "test", 0,
> QApplication::UnicodeUTF8));
> menuTest2->setTitle(QApplication::translate("MainWindow", "test2", 0,
> QApplication::UnicodeUTF8));
> } // retranslateUi
>
> };
>
> namespace Ui {
> class MainWindow: public Ui_MainWindow {};
> } // namespace Ui
>
> QT_END_NAMESPACE
>
> #endif // UI_MAINWINDOW_H
>
> qtdesign3.cpp:
> #include <QtGui>
>
> #include "qtdesign3.h"
> #include "test1.h"
>
> MainWindow::MainWindow() :
> QMainWindow (0,Qt::Window)
> {
> ui.setupUi(this);
> Test1 test(ui.centralwidget);
> QMainWindow::setCentralWidget(&test);
> }
>
> test1.h:
> #include "ui_test1.h"
>
> class Test1 :public QWidget {
> Q_OBJECT
>
> public:
> Test1(QWidget *parent = 0);
> private:
> Ui::Test1 t1;
> };
>
> test1.cpp:
> #include <QtGui>
> #include "test1.h"
>
> Test1::Test1(QWidget *parent)
> {
> t1.setupUi(this);
> }
>
> ui_test1.h:
> #ifndef UI_TEST1_H
> #define UI_TEST1_H
>
> #include <QtCore/QVariant>
> #include <QtGui/QAction>
> #include <QtGui/QApplication>
> #include <QtGui/QButtonGroup>
> #include <QtGui/QGridLayout>
> #include <QtGui/QGroupBox>
> #include <QtGui/QLineEdit>
> #include <QtGui/QPushButton>
> #include <QtGui/QSpinBox>
> #include <QtGui/QTextEdit>
> #include <QtGui/QWidget>
>
> QT_BEGIN_NAMESPACE
>
> class Ui_Test1
> {
> public:
> QGridLayout *gridLayout;
> QPushButton *pushButton;
> QPushButton *pushButton_2;
> QGroupBox *groupBox;
> QLineEdit *lineEdit;
> QTextEdit *textEdit;
> QSpinBox *spinBox;
>
> void setupUi(QWidget *Test1)
> {
> if (Test1->objectName().isEmpty())
> Test1->setObjectName(QString::fromUtf8("Test1"));
> Test1->resize(724, 442);
> gridLayout = new QGridLayout(Test1);
> gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
> pushButton = new QPushButton(Test1);
> pushButton->setObjectName(QString::fromUtf8("pushButton"));
>
> gridLayout->addWidget(pushButton, 0, 0, 1, 1);
>
> pushButton_2 = new QPushButton(Test1);
> pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
>
> gridLayout->addWidget(pushButton_2, 2, 0, 1, 1);
>
> groupBox = new QGroupBox(Test1);
> groupBox->setObjectName(QString::fromUtf8("groupBox"));
> lineEdit = new QLineEdit(groupBox);
> lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
> lineEdit->setGeometry(QRect(30, 30, 113, 25));
> textEdit = new QTextEdit(groupBox);
> textEdit->setObjectName(QString::fromUtf8("textEdit"));
> textEdit->setGeometry(QRect(30, 100, 104, 94));
> spinBox = new QSpinBox(groupBox);
> spinBox->setObjectName(QString::fromUtf8("spinBox"));
> spinBox->setGeometry(QRect(210, 170, 50, 25));
>
> gridLayout->addWidget(groupBox, 1, 0, 1, 1);
>
>
> retranslateUi(Test1);
>
> QMetaObject::connectSlotsByName(Test1);
> } // setupUi
>
> void retranslateUi(QWidget *Test1)
> {
> Test1->setWindowTitle(QApplication::translate("Test1", "Test1", 0,
> QApplication::UnicodeUTF8));
> pushButton->setText(QApplication::translate("Test1", "test button1", 0,
> QApplication::UnicodeUTF8));
> pushButton_2->setText(QApplication::translate("Test1", "test button2",
> 0, QApplication::UnicodeUTF8));
> groupBox->setTitle(QApplication::translate("Test1", "grupa1", 0,
> QApplication::UnicodeUTF8));
> Q_UNUSED(Test1);
> } // retranslateUi
>
> };
>
> namespace Ui {
> class Test1: public Ui_Test1 {};
> } // namespace Ui
>
> QT_END_NAMESPACE
>
> #endif // UI_TEST1_H
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 236 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment.bin
-------------- next part --------------
######################################################################
# Automatically generated by qmake (2.01a) ?r. lis 18 22:16:27 2009
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += . forms
INCLUDEPATH += .
# Input
HEADERS += qtdesign5.h stacked.h
FORMS += forms/mainwindow.ui forms/stacked.ui
SOURCES += main.cpp qtdesign5.cpp stacked.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stacked.cpp
Type: text/x-c++src
Size: 134 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qtdesign5.cpp
Type: text/x-c++src
Size: 411 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment-0002.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qtdesign5.h
Type: text/x-c++hdr
Size: 218 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment-0003.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stacked.h
Type: text/x-c++hdr
Size: 208 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment-0004.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mainwindow.ui
Type: application/x-designer
Size: 1281 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment-0005.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stacked.ui
Type: application/x-designer
Size: 1377 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/19eed568/attachment-0006.bin
More information about the Qt-interest-old
mailing list