Qt-interest Archive, May 2008
Newbie Trying to Learn QStackedWidget
Pages: Prev | 1 | 2 | Next
Message 16 in thread
Please repsond to the list...
You have now left the QT issues list, and are entering into the C++
list...
Basically, you need to define your variables in a larger scope. Since
the moment your constructor is over, they are deleted.
In your MainForm class definition, add the following.
Form1 * form1;
Form2 * form2;
QStackedWidget * stackedWidget;
Then use the code I sent out previously...
I HIGHLY recommend you buy a c++ book...
Scott
> -----Original Message-----
> From: Peter E Dennis [mailto:peter.edwin.dennis@xxxxxxxxx]
> Sent: Thursday, May 29, 2008 1:17 AM
> To: Scott Aron Bloom
> Subject: Re: Newbie Trying to Learn QStackedWidget
>
> Hi Scott,
>
> Thanks for your reply.
>
> I have tried a couple of ways to do this, when I add your suggestion
> and try to 'make' it again:
>
> mainwindow.cpp: In constructor `MainWindow::MainWindow(QWidget*)':
> mainwindow.cpp:16: error: `form1' undeclared (first use this function)
> mainwindow.cpp:16: error: (Each undeclared identifier is reported only
> once for
> each function it appears in.)
> mainwindow.cpp:17: error: `form2' undeclared (first use this function)
> mingw32-make[1]: *** [tmp/obj/release_shared/mainwindow.o] Error 1
> mingw32-make[1]: Leaving directory `C:/Qt/4.4.0/projects/Stacked'
> mingw32-make: *** [release] Error 2
>
> if I then put:
>
> Form1 form1 = new Form1;
> Form2 form2 = new Form2;
>
> stackedWidget = new QStackedWidget( this );
> stackedWidget->addWidget( form1 );
> stackedWidget->addWidget( form2 );
>
> I get the following errors:
>
> C:\Qt\4.4.0\projects\Stacked>make
> mingw32-make -f Makefile.Release
> mingw32-make[1]: Entering directory `C:/Qt/4.4.0/projects/Stacked'
> g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -
> DQT_LARGEFILE_SUPPORT
> -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT
-
> DQT_NEEDS
> _QMAIN -I"..\..\include\QtCore" -I"..\..\include\QtCore" -
> I"..\..\include\QtGui"
> -I"..\..\include\QtGui" -I"..\..\include" -I"." -
> I"c:\Qt\4.4.0\include\ActiveQt
> " -I"tmp\moc\release_shared" -I"." -I"..\..\mkspecs\win32-g++" -o
> tmp\obj\releas
> e_shared\mainwindow.o mainwindow.cpp
> ../../include/QtGui/../../src/gui/kernel/qwidget.h: In copy
constructor
> `Form1::
> Form1(const Form1&)':
> ../../include/QtGui/../../src/gui/kernel/qwidget.h:750: error:
> `QWidget::QWidget
> (const QWidget&)' is private
> mainwindow.cpp:16: error: within this context
> mainwindow.cpp: In constructor `MainWindow::MainWindow(QWidget*)':
> mainwindow.cpp:16: error: initializing temporary from result of
> `Form1::Form1(
> QWidget*)'
> ../../include/QtGui/../../src/gui/kernel/qwidget.h: In copy
constructor
> `Form2::
> Form2(const Form2&)':
> ../../include/QtGui/../../src/gui/kernel/qwidget.h:750: error:
> `QWidget::QWidget
> (const QWidget&)' is private
> mainwindow.cpp:17: error: within this context
> mainwindow.cpp: In constructor `MainWindow::MainWindow(QWidget*)':
> mainwindow.cpp:17: error: initializing temporary from result of
> `Form2::Form2(
> QWidget*)'
> mainwindow.cpp:20: error: no matching function for call to
> `QStackedWidget::addW
> idget(Form1&)'
> ../../include/QtGui/../../src/gui/widgets/qstackedwidget.h:69: note:
> candidates
> are: int QStackedWidget::addWidget(QWidget*)
> mainwindow.cpp:21: error: no matching function for call to
> `QStackedWidget::addW
> idget(Form2&)'
> ../../include/QtGui/../../src/gui/widgets/qstackedwidget.h:69: note:
> candidates
> are: int QStackedWidget::addWidget(QWidget*)
> mingw32-make[1]: *** [tmp/obj/release_shared/mainwindow.o] Error 1
> mingw32-make[1]: Leaving directory `C:/Qt/4.4.0/projects/Stacked'
> mingw32-make: *** [release] Error 2
>
> C:\Qt\4.4.0\projects\Stacked>
>
> Thanks again,
>
> Peter.
>
>
> 2008/5/29 Scott Aron Bloom <Scott.Bloom@xxxxxxxxxxxx>:
> > Inyour MainWindow cosntructor... Create 2 forms, and the stacked
> > widget...
> >
> > Ie
> > form1 = new Form1;
> > form2 = new Form2;
> >
> > stackedWidget = new QStackedWidget( this );
> > stackedWidget->addWidget( form1 );
> > stackedWidget->addWidget( form2 );
> >
> >
> > Scott
> >
> >
> >
> >> -----Original Message-----
> >> From: Peter E Dennis [mailto:peter.edwin.dennis@xxxxxxxxx]
> >> Sent: Thursday, May 29, 2008 12:18 AM
> >> To: qt-interest@xxxxxxxxxxxxx
> >> Subject: Newbie Trying to Learn QStackedWidget
> >>
> >> Hi,
> >>
> >> I am new to C++ and Qt and am trying to learn the basics of a
> >> QStackedWidget. All I'm trying to do at this stage is add a couple
> of
> >> basic forms to the QStackedWidget and then display each form when a
> >> menu item is chosen.
> >>
> >> To do this I have copied and adapted the following code from books
> and
> >> examples:
> >> /*********
> >> * Form1.h
> >> *********/
> >> #include <QtGui>
> >> #include "ui_Form1.h"
> >>
> >> class Form1 : public QWidget,
> >> public Ui::Form1
> >> {
> >>
> >> Q_OBJECT
> >>
> >> public:
> >> Form1(QWidget* parent = 0);
> >> ~Form1();
> >>
> >> };
> >>
> >> /***********
> >> * Form1.cpp
> >> ***********/
> >> #include <QWidget>
> >> #include "Form1.h"
> >>
> >> Form1::Form1(QWidget* parent)
> >> {
> >> setupUi(this);
> >> }
> >>
> >> Form1::~Form1()
> >> {
> >>
> >> }
> >>
> >> /*********
> >> * Form2.h
> >> *********/
> >> #include <QtGui>
> >> #include "ui_Form2.h"
> >>
> >> class Form2 : public QWidget,
> >> public Ui::Form2
> >> {
> >>
> >> Q_OBJECT
> >>
> >> public:
> >> Form2(QWidget* parent = 0);
> >> ~Form2();
> >>
> >> };
> >>
> >> /***********
> >> * Form2.cpp
> >> ***********/
> >> #include <QWidget>
> >> #include "Form2.h"
> >>
> >> Form2::Form2(QWidget* parent)
> >> {
> >> setupUi(this);
> >> }
> >>
> >> Form2::~Form2()
> >> {
> >>
> >> }
> >>
> >> /**************
> >> * mainwindow.h
> >> **************/
> >> #ifndef MAINWINDOW_H
> >> #define MAINWINDOW_H
> >>
> >> #include <QMainWindow>
> >> #include "ui_Stacked.h"
> >>
> >> class MainWindow : public QMainWindow,
> >> public Ui::MainWindow
> >> {
> >> Q_OBJECT
> >> public:
> >> MainWindow(QWidget* parent = 0);
> >> ~MainWindow();
> >>
> >> protected:
> >> void setupActions();
> >> protected slots:
> >> void showForm1();
> >> void showForm2();
> >> };
> >> #endif // MAINWINDOW_H
> >>
> >> /****************
> >> * mainwindow.cpp
> >> ****************/
> >> #include <QtGui>
> >> #include "mainwindow.h"
> >> #include "Form1.h"
> >> #include "Form2.h"
> >>
> >> // Constructor
> >> MainWindow::MainWindow(QWidget* parent)
> >> : QMainWindow(parent)
> >> {
> >> setupUi(this);
> >> setupActions();
> >>
> >> }
> >>
> >> // Destructor
> >> MainWindow::~MainWindow()
> >> {
> >>
> >> }
> >>
> >> void MainWindow::setupActions()
> >> {
> >> connect(actionShow_Form_1, SIGNAL(triggered(bool)),
> >> qApp, SLOT(showForm1()));
> >> connect(actionShow_Form_2, SIGNAL(triggered(bool)),
> >> qApp, SLOT(showForm2()));
> >>
> >> }
> >>
> >> void MainWindow::showForm1()
> >> {
> >> stackedWidget->setCurrentIndex(0);
> >> }
> >>
> >> void MainWindow::showForm2()
> >> {
> >> stackedWidget->setCurrentIndex(1);
> >> }
> >>
> >> /**********
> >> * main.cpp
> >> **********/
> >> #include <QApplication>
> >> #include "mainwindow.h"
> >>
> >> int main(int argc, char *argv[])
> >> {
> >> QApplication a(argc, argv);
> >>
> >> MainWindow mainWindow;
> >> mainWindow.show();
> >>
> >> return a.exec();
> >> }
> >>
> >> Could someone please tell me where I add each Form to the
> >> QStackedWidget? I have tried various things as part of the
> MainWindow
> >> constructor but get errors with whatever I try.
> >>
> >> Many thanks,
> >>
> >> Peter.
> >>
> >> --
> >> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> with
> >> "unsubscribe" in the subject or the body.
> >> List archive and information: http://lists.trolltech.com/qt-
> interest/
> >
> >
--
[ signature omitted ]
Message 17 in thread
Thanks Scott. I tried as you suggested but like I mentioned before
there is already a QStackedWidget defined in the ui_Stacked.h file so
defining one again seems to play havoc with how it is displayed.
But like you say I really don't understand the C++ concepts let alone
how to apply them to Qt.
Can anyone recommend a good C++ book that will help me then move onto Qt?
Thanks again to all who've helped.
Peter.
2008/5/30 Scott Aron Bloom <Scott.Bloom@xxxxxxxxxxxx>:
> Please repsond to the list...
>
> You have now left the QT issues list, and are entering into the C++
> list...
>
> Basically, you need to define your variables in a larger scope. Since
> the moment your constructor is over, they are deleted.
>
> In your MainForm class definition, add the following.
>
> Form1 * form1;
> Form2 * form2;
> QStackedWidget * stackedWidget;
>
> Then use the code I sent out previously...
>
> I HIGHLY recommend you buy a c++ book...
>
>
> Scott
>
>
>
>> -----Original Message-----
>> From: Peter E Dennis [mailto:peter.edwin.dennis@xxxxxxxxx]
>> Sent: Thursday, May 29, 2008 1:17 AM
>> To: Scott Aron Bloom
>> Subject: Re: Newbie Trying to Learn QStackedWidget
>>
>> Hi Scott,
>>
>> Thanks for your reply.
>>
>> I have tried a couple of ways to do this, when I add your suggestion
>> and try to 'make' it again:
>>
>> mainwindow.cpp: In constructor `MainWindow::MainWindow(QWidget*)':
>> mainwindow.cpp:16: error: `form1' undeclared (first use this function)
>> mainwindow.cpp:16: error: (Each undeclared identifier is reported only
>> once for
>> each function it appears in.)
>> mainwindow.cpp:17: error: `form2' undeclared (first use this function)
>> mingw32-make[1]: *** [tmp/obj/release_shared/mainwindow.o] Error 1
>> mingw32-make[1]: Leaving directory `C:/Qt/4.4.0/projects/Stacked'
>> mingw32-make: *** [release] Error 2
>>
>> if I then put:
>>
>> Form1 form1 = new Form1;
>> Form2 form2 = new Form2;
>>
>> stackedWidget = new QStackedWidget( this );
>> stackedWidget->addWidget( form1 );
>> stackedWidget->addWidget( form2 );
>>
>> I get the following errors:
>>
>> C:\Qt\4.4.0\projects\Stacked>make
>> mingw32-make -f Makefile.Release
>> mingw32-make[1]: Entering directory `C:/Qt/4.4.0/projects/Stacked'
>> g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -
>> DQT_LARGEFILE_SUPPORT
>> -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT
> -
>> DQT_NEEDS
>> _QMAIN -I"..\..\include\QtCore" -I"..\..\include\QtCore" -
>> I"..\..\include\QtGui"
>> -I"..\..\include\QtGui" -I"..\..\include" -I"." -
>> I"c:\Qt\4.4.0\include\ActiveQt
>> " -I"tmp\moc\release_shared" -I"." -I"..\..\mkspecs\win32-g++" -o
>> tmp\obj\releas
>> e_shared\mainwindow.o mainwindow.cpp
>> ../../include/QtGui/../../src/gui/kernel/qwidget.h: In copy
> constructor
>> `Form1::
>> Form1(const Form1&)':
>> ../../include/QtGui/../../src/gui/kernel/qwidget.h:750: error:
>> `QWidget::QWidget
>> (const QWidget&)' is private
>> mainwindow.cpp:16: error: within this context
>> mainwindow.cpp: In constructor `MainWindow::MainWindow(QWidget*)':
>> mainwindow.cpp:16: error: initializing temporary from result of
>> `Form1::Form1(
>> QWidget*)'
>> ../../include/QtGui/../../src/gui/kernel/qwidget.h: In copy
> constructor
>> `Form2::
>> Form2(const Form2&)':
>> ../../include/QtGui/../../src/gui/kernel/qwidget.h:750: error:
>> `QWidget::QWidget
>> (const QWidget&)' is private
>> mainwindow.cpp:17: error: within this context
>> mainwindow.cpp: In constructor `MainWindow::MainWindow(QWidget*)':
>> mainwindow.cpp:17: error: initializing temporary from result of
>> `Form2::Form2(
>> QWidget*)'
>> mainwindow.cpp:20: error: no matching function for call to
>> `QStackedWidget::addW
>> idget(Form1&)'
>> ../../include/QtGui/../../src/gui/widgets/qstackedwidget.h:69: note:
>> candidates
>> are: int QStackedWidget::addWidget(QWidget*)
>> mainwindow.cpp:21: error: no matching function for call to
>> `QStackedWidget::addW
>> idget(Form2&)'
>> ../../include/QtGui/../../src/gui/widgets/qstackedwidget.h:69: note:
>> candidates
>> are: int QStackedWidget::addWidget(QWidget*)
>> mingw32-make[1]: *** [tmp/obj/release_shared/mainwindow.o] Error 1
>> mingw32-make[1]: Leaving directory `C:/Qt/4.4.0/projects/Stacked'
>> mingw32-make: *** [release] Error 2
>>
>> C:\Qt\4.4.0\projects\Stacked>
>>
>> Thanks again,
>>
>> Peter.
>>
>>
>> 2008/5/29 Scott Aron Bloom <Scott.Bloom@xxxxxxxxxxxx>:
>> > Inyour MainWindow cosntructor... Create 2 forms, and the stacked
>> > widget...
>> >
>> > Ie
>> > form1 = new Form1;
>> > form2 = new Form2;
>> >
>> > stackedWidget = new QStackedWidget( this );
>> > stackedWidget->addWidget( form1 );
>> > stackedWidget->addWidget( form2 );
>> >
>> >
>> > Scott
>> >
>> >
>> >
>> >> -----Original Message-----
>> >> From: Peter E Dennis [mailto:peter.edwin.dennis@xxxxxxxxx]
>> >> Sent: Thursday, May 29, 2008 12:18 AM
>> >> To: qt-interest@xxxxxxxxxxxxx
>> >> Subject: Newbie Trying to Learn QStackedWidget
>> >>
>> >> Hi,
>> >>
>> >> I am new to C++ and Qt and am trying to learn the basics of a
>> >> QStackedWidget. All I'm trying to do at this stage is add a couple
>> of
>> >> basic forms to the QStackedWidget and then display each form when a
>> >> menu item is chosen.
>> >>
>> >> To do this I have copied and adapted the following code from books
>> and
>> >> examples:
>> >> /*********
>> >> * Form1.h
>> >> *********/
>> >> #include <QtGui>
>> >> #include "ui_Form1.h"
>> >>
>> >> class Form1 : public QWidget,
>> >> public Ui::Form1
>> >> {
>> >>
>> >> Q_OBJECT
>> >>
>> >> public:
>> >> Form1(QWidget* parent = 0);
>> >> ~Form1();
>> >>
>> >> };
>> >>
>> >> /***********
>> >> * Form1.cpp
>> >> ***********/
>> >> #include <QWidget>
>> >> #include "Form1.h"
>> >>
>> >> Form1::Form1(QWidget* parent)
>> >> {
>> >> setupUi(this);
>> >> }
>> >>
>> >> Form1::~Form1()
>> >> {
>> >>
>> >> }
>> >>
>> >> /*********
>> >> * Form2.h
>> >> *********/
>> >> #include <QtGui>
>> >> #include "ui_Form2.h"
>> >>
>> >> class Form2 : public QWidget,
>> >> public Ui::Form2
>> >> {
>> >>
>> >> Q_OBJECT
>> >>
>> >> public:
>> >> Form2(QWidget* parent = 0);
>> >> ~Form2();
>> >>
>> >> };
>> >>
>> >> /***********
>> >> * Form2.cpp
>> >> ***********/
>> >> #include <QWidget>
>> >> #include "Form2.h"
>> >>
>> >> Form2::Form2(QWidget* parent)
>> >> {
>> >> setupUi(this);
>> >> }
>> >>
>> >> Form2::~Form2()
>> >> {
>> >>
>> >> }
>> >>
>> >> /**************
>> >> * mainwindow.h
>> >> **************/
>> >> #ifndef MAINWINDOW_H
>> >> #define MAINWINDOW_H
>> >>
>> >> #include <QMainWindow>
>> >> #include "ui_Stacked.h"
>> >>
>> >> class MainWindow : public QMainWindow,
>> >> public Ui::MainWindow
>> >> {
>> >> Q_OBJECT
>> >> public:
>> >> MainWindow(QWidget* parent = 0);
>> >> ~MainWindow();
>> >>
>> >> protected:
>> >> void setupActions();
>> >> protected slots:
>> >> void showForm1();
>> >> void showForm2();
>> >> };
>> >> #endif // MAINWINDOW_H
>> >>
>> >> /****************
>> >> * mainwindow.cpp
>> >> ****************/
>> >> #include <QtGui>
>> >> #include "mainwindow.h"
>> >> #include "Form1.h"
>> >> #include "Form2.h"
>> >>
>> >> // Constructor
>> >> MainWindow::MainWindow(QWidget* parent)
>> >> : QMainWindow(parent)
>> >> {
>> >> setupUi(this);
>> >> setupActions();
>> >>
>> >> }
>> >>
>> >> // Destructor
>> >> MainWindow::~MainWindow()
>> >> {
>> >>
>> >> }
>> >>
>> >> void MainWindow::setupActions()
>> >> {
>> >> connect(actionShow_Form_1, SIGNAL(triggered(bool)),
>> >> qApp, SLOT(showForm1()));
>> >> connect(actionShow_Form_2, SIGNAL(triggered(bool)),
>> >> qApp, SLOT(showForm2()));
>> >>
>> >> }
>> >>
>> >> void MainWindow::showForm1()
>> >> {
>> >> stackedWidget->setCurrentIndex(0);
>> >> }
>> >>
>> >> void MainWindow::showForm2()
>> >> {
>> >> stackedWidget->setCurrentIndex(1);
>> >> }
>> >>
>> >> /**********
>> >> * main.cpp
>> >> **********/
>> >> #include <QApplication>
>> >> #include "mainwindow.h"
>> >>
>> >> int main(int argc, char *argv[])
>> >> {
>> >> QApplication a(argc, argv);
>> >>
>> >> MainWindow mainWindow;
>> >> mainWindow.show();
>> >>
>> >> return a.exec();
>> >> }
>> >>
>> >> Could someone please tell me where I add each Form to the
>> >> QStackedWidget? I have tried various things as part of the
>> MainWindow
>> >> constructor but get errors with whatever I try.
>> >>
>> >> Many thanks,
>> >>
>> >> Peter.
>> >>
>> >> --
>> >> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
>> with
>> >> "unsubscribe" in the subject or the body.
>> >> List archive and information: http://lists.trolltech.com/qt-
>> interest/
>> >
>> >
>
--
[ signature omitted ]