Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, February 2008
[stackedWidgets] they do not show up ..


Message 1 in thread

Hi group,

I am playing around with stacked widgets the ultimate goal being to get
the "connectSlotByName" - mechanism work.

Now the attached code is a simple application where I use two - very
similar - .ui files for stacking on a mainwindow-application.

it shows the main widgets. however the .ui widgets do not show up ...

What did I do wrong? The attached code compiles nicely and runs but ...

tia Jörg
######################################################################
######################################################################
# Automatically generated by qmake (2.01a) Sa Dez 15 12:41:17 2007
######################################################################

CONFIG   += debug
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += connectcheck.h secondform.h mainwindow.h
FORMS += connectcheck.ui \
         secondform.ui
SOURCES += connectcheck.cpp main.cpp secondform.cpp mainwindow.cpp
/* check connect signal - slot mechanism
/* check connect signal - slot mechanism
 * 2007-12-15 implementation part
*/

#include <QDialog>
#include <iostream>

#include "connectcheck.h"

ConnectCheck::ConnectCheck( QWidget *parent = 0 ) {
	setupUi( this ) ;
	std::cout << "Constructor ConnectCheck" << std::endl ;
}
ConnectCheck::~ConnectCheck(){
}

void ConnectCheck::on_TestMeButton_clicked() {
	std::cout << "TestMeButton clicked" << std::endl ;
}

// override "connectNotify" function of QObject ...
#ifdef TRIAL_CODE
	void ConnectCheck::connectNotify( const char* signal ) {

	std::cout << "Connection notification" << std::endl ;
}
#endif
/* check connect signal - slot mechanism
/* check connect signal - slot mechanism
 * 2007-12-15 definition part
*/

#include <QtGui>

#include "ui_connectcheck.h"

class ConnectCheck : public QDialog, public Ui::Dialog
{
	Q_OBJECT
	public:
		ConnectCheck( QWidget * );
		~ConnectCheck();
//		virtual void connectNotify( const char * signal ) ;
	public slots:
		void on_TestMeButton_clicked() ;
} ;

Attachment:

Attachment: connectcheck.ui
Description: application/designer

#include <QApplication>

#include "mainwindow.h"

int main( int argc, char *argv[] )
{
	QApplication app( argc, argv ) ;
	MainWindow *mainwindow = new MainWindow ;
	mainwindow->show() ;
	return app.exec();
}
#include <QtGui>

#include "mainwindow.h"
#include "connectcheck.h"
#include "secondform.h"

MainWindow::MainWindow()
{

	// JK1 begin

	connectCheck = new ConnectCheck( this ) ;		// construct connectCheckForm
	secondForm = new SecondForm( this ) ; 			// construct secondForm ...
											 // incl. "connect's"
	QWidget *connectCheckWidget = new QWidget ;
	QWidget *secondFormWidget = new QWidget ;

	// prepare for at least two pages:
	// one is connectCheck
	// the other is secondForm

	stackedWidget = new QStackedWidget ;	// here we start with
	stackedWidget->addWidget( connectCheckWidget ) ;		// widget-stack
	stackedWidget->addWidget( secondFormWidget ) ;			// and have added two widgets to stack

	// build main layouts ...

	setCentralWidget( stackedWidget );     					// set it as central widget ...
	createActions();
	createMenus();
	createContextMenu();
	createToolBars();

	// switch from one widget to the other ...

	// JK 1 end

}
void MainWindow::createActions()
{
	// JK1 begin
	exitAction = new QAction(tr("E&xit"), this);
	exitAction->setShortcut(tr("Ctrl+Q"));
	exitAction->setStatusTip(tr("Exit the application"));
	connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); // close is taken from QWidget


	toggleWindowAction = new QAction(tr("&Toggle Window..."), this);
	toggleWindowAction->setIcon(QIcon(":/images/gotocell.png"));
	toggleWindowAction->setShortcut(tr("F12"));
	toggleWindowAction->setStatusTip(tr("Toggle between Geodata-window and result window"));
	toggleIndex = 0 ;
	connect(toggleWindowAction, SIGNAL(triggered()),
			this, SLOT(toggleWindow()));
	// JK1 end
}

void MainWindow::createMenus()
{

	toolsMenu = menuBar()->addMenu(tr("&Tools"));
	toolsMenu->addAction( toggleWindowAction ) ; // JK1
	toolsMenu->addSeparator();
	toolsMenu->addAction(exitAction);

}

void MainWindow::createContextMenu()
{
	connectCheck->setContextMenuPolicy(Qt::ActionsContextMenu);
	secondForm->setContextMenuPolicy(Qt::ActionsContextMenu);
}


void MainWindow::createToolBars()
{

}

void MainWindow::toggleWindow()   // toggles between connectCheck-widget and secondFormWidget
{
	if( toggleIndex ) {
		toggleIndex = 0 ;
		// connectCheck = new ConnectCheck( this ) ; // temp - no  effect
	}else {
		toggleIndex = 1 ;
		// secondForm = new SecondForm( this ) ;		 // temp - no effect
	}
	stackedWidget->setCurrentIndex( toggleIndex ) ;
	std::cout << "Widget Index = " << toggleIndex << std::endl ;

}

#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <iostream>
#include "ui_connectcheck.h"
#include "ui_secondform.h"
class ConnectCheck ;
class SecondForm ;

class MainWindow : public QMainWindow, public Ui::secondForm, public Ui::Dialog
{
	Q_OBJECT
	public:
		MainWindow() ;

		// JK1 begin
		// the stacked items (presently 2):
	private slots:
		void toggleWindow() ; // JK1
	private:
		QStackedWidget *stackedWidget ;
		void createContextMenu() ;
		void createToolBars() ;

		int 	toggleIndex ;
		ConnectCheck *connectCheck ; 		// index = 0
		SecondForm *secondForm ;	// index = 1
		QAction *toggleWindowAction ;
		QMenu *toolsMenu;
		QAction *exitAction;

		void createActions() ;
		void createMenus() ;


		// JK1 end

} ;
#endif
This is a small routine which tests the automated connect mechanism of Qt4
This is a small routine which tests the automated connect mechanism of Qt4
However this is version 2 of it using stacked widgets and connectSlotByName from push buttons of the
individual different stacked widgets ...

the stacked widgets are:
 SecondForm
 and:
 ConnectCheck
 organisation within "mainwindow.cpp/h"
tests "connect" and "connectNotify"
/* check connect signal - slot mechanism
 * 2007-12-15 implementation part
*/

#include <QWidget>
#include <iostream>

#include "secondform.h"

SecondForm::SecondForm( QWidget *parent = 0 ) {
	setupUi( this ) ;
	std::cout << "Constructor SecondForm" << std::endl ;

}
SecondForm::~SecondForm(){
}

void SecondForm::on_secondPushButton_clicked() {
	std::cout << "Second Push Button clicked" << std::endl ;
}

// override "connectNotify" function of QObject ...
#ifdef FUTURE
	void SecondForm::connectNotify( const char* signal ) {

		std::cout << "Connection notification" << std::endl ;
	}
#endif
/* check connect signal - slot mechanism on stacked widgets
/* check connect signal - slot mechanism on stacked widgets
 * 2007-12-15 definition part
*/
#ifndef SECOND_FORM_H
#define SECOND_FORM_H
#include <QtGui>

#include "ui_secondform.h"

class SecondForm : public QWidget, public Ui::secondForm
{
	Q_OBJECT
	public:
		SecondForm( QWidget * );
		~SecondForm();
//		virtual void connectNotify( const char * signal ) ;
	public slots:
		void on_secondPushButton_clicked() ;
} ;
#endif

Attachment:

Attachment: secondform.ui
Description: application/designer


Message 2 in thread

On 07.02.08 22:33:01, Ionathan wrote:
> Hi group,
> 
> I am playing around with stacked widgets the ultimate goal being to get
> the "connectSlotByName" - mechanism work.
> 
> Now the attached code is a simple application where I use two - very
> similar - .ui files for stacking on a mainwindow-application.
> 
> it shows the main widgets. however the .ui widgets do not show up ...
> 
> What did I do wrong? The attached code compiles nicely and runs but ...

About everything. Sorry. I know you're probably just starting, but you
really need to go back to the start and read again the designer manual
and possibly also check some of the examples. 

A few things that are wrong:

- mainwindow inherits from both .ui files
- the 2 classes that use the .ui files, are both QDialogs and thus
  themselves top level windows, not embedded anywhere
- you create 2 empty QWidgets and put those into the stack, how can you
  expect them to show anything, except their background.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

Andreas Pakulat schrieb:
> On 07.02.08 22:33:01, Ionathan wrote:
>   
>> Hi group,
>>
>> I am playing around with stacked widgets the ultimate goal being to get
>> the "connectSlotByName" - mechanism work.
>>
>> Now the attached code is a simple application where I use two - very
>> similar - .ui files for stacking on a mainwindow-application.
>>
>> it shows the main widgets. however the .ui widgets do not show up ...
>>
>> What did I do wrong? The attached code compiles nicely and runs but ...
>>     
>
> About everything. Sorry. I know you're probably just starting, but you
> really need to go back to the start and read again the designer manual
> and possibly also check some of the examples. 
>
> A few things that are wrong:
>
> - mainwindow inherits from both .ui files
> - the 2 classes that use the .ui files, are both QDialogs and thus
>   themselves top level windows, not embedded anywhere
> - you create 2 empty QWidgets and put those into the stack, how can you
>   expect them to show anything, except their background.
>
>   
Thanks Andreas, I was a bit absent, now in the meantime I got something
to work better, however it still does not switch the widgets ... If you
would be so kind as to look over the attached files? should not be too
much work ... tia Jorge

/* check connect signal - slot mechanism
/* check connect signal - slot mechanism
 * 2007-12-15 implementation part
*/

#include <QWidget>
#include <iostream>

#include "firstform.h"

ConnectCheck::ConnectCheck( QWidget *parent = 0 ) {
	setupUi( parent ) ;
	std::cout << "Constructor ConnectCheck" << std::endl ;
}
ConnectCheck::~ConnectCheck(){
}
#ifdef TRIAL_CODE
void ConnectCheck::on_TestMeButton_clicked() {
	std::cout << "TestMeButton clicked" << std::endl ;
}
#endif
// override "connectNotify" function of QObject ...
#ifdef TRIAL_CODE
	void ConnectCheck::connectNotify( const char* signal ) {

	std::cout << "Connection notification" << std::endl ;
}
#endif

/* check connect signal - slot mechanism
/* check connect signal - slot mechanism
 * 2007-12-15 definition part
*/

#include <QtGui>

#include "ui_firstform.h"

class ConnectCheck : public Ui::PrimaryForm, public QWidget
{
//	Q_OBJECT
	public:
		ConnectCheck( QWidget * );
		~ConnectCheck();
//		virtual void connectNotify( const char * signal ) ;
//	public slots:
//		void on_TestMeButton_clicked() ;
} ;

Attachment:

Attachment: firstform.ui
Description: application/designer

#include <QApplication>

#include "mainwindow.h"

int main( int argc, char *argv[] )
{
	QApplication app( argc, argv ) ;
	MainWindow *mainwindow = new MainWindow ;
	mainwindow->show() ;
	return app.exec();
}

#include <QtGui>

#include "mainwindow.h"
#include "firstform.h"
#include "secondform.h"

MainWindow::MainWindow()
{

	// JK1 begin

	connectCheck = new ConnectCheck( this ) ;		// construct connectCheckForm
	secondForm = new SecondForm( this ) ; 			// construct secondForm ...
											 // incl. "connect's"
//	QWidget *connectCheckWidget = new QWidget ;
//	QWidget *secondFormWidget = new QWidget ;

	// prepare for at least two pages:
	// one is connectCheck
	// the other is secondForm

	stackedWidget = new QStackedWidget ;	// here we start with
	stackedWidget->addWidget( connectCheck ) ;		// widget-stack
	stackedWidget->addWidget( secondForm ) ;			// and have added two widgets to stack
	stackedWidget->setCurrentIndex( 0 ) ;	// begin with first widget

	// build main layouts ...

	setCentralWidget( stackedWidget ); 		// set it as central widget ...
	createActions();
	createMenus();
	createContextMenu();
	createToolBars();
}

void MainWindow::createActions()
{
	exitAction = new QAction(tr("E&xit"), this);
	exitAction->setShortcut(tr("Ctrl+Q"));
	exitAction->setStatusTip(tr("Exit the application"));
	connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); // close is taken from QWidget


	toggleWindowAction = new QAction(tr("&Toggle Window..."), this);
	toggleWindowAction->setIcon(QIcon(":/images/gotocell.png"));
	toggleWindowAction->setShortcut(tr("F12"));
	toggleWindowAction->setStatusTip(tr("Toggle between Geodata-window and result window"));
	toggleIndex = 0 ;
	connect(toggleWindowAction, SIGNAL(triggered()),
			this, SLOT(toggleWindow()));
}

void MainWindow::createMenus()
{

	toolsMenu = menuBar()->addMenu(tr("&Tools"));
	toolsMenu->addAction( toggleWindowAction ) ; // JK1
	toolsMenu->addSeparator();
	toolsMenu->addAction(exitAction);

}

void MainWindow::createContextMenu()
{
	connectCheck->setContextMenuPolicy(Qt::ActionsContextMenu);
	secondForm->setContextMenuPolicy(Qt::ActionsContextMenu);
}


void MainWindow::createToolBars()
{

}

void MainWindow::toggleWindow()   // toggles between connectCheck-widget and secondFormWidget
{
	if( toggleIndex ) {
		toggleIndex = 0 ;
	}else {
		toggleIndex = 1 ;
	}
	stackedWidget->setCurrentIndex( toggleIndex ) ;
	stackedWidget->update() ;
	std::cout << "Widget Index = " << toggleIndex <<
			" - and widget has = "<< stackedWidget->currentIndex() << std::endl ;

}


#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <iostream>
#include "ui_firstform.h"
#include "ui_secondform.h"
class ConnectCheck ;
class SecondForm ;

class MainWindow : public QMainWindow //, public Ui::secondForm, public Ui::Dialog
{
	Q_OBJECT
	public:
		MainWindow() ;

		// JK1 begin
		// the stacked items (presently 2):
	private slots:
		void toggleWindow() ; // JK1
	private:
		QStackedWidget *stackedWidget ;
		void createContextMenu() ;
		void createToolBars() ;

		int 	toggleIndex ;
		ConnectCheck *connectCheck ; 		// index = 0
		SecondForm *secondForm ;	// index = 1
		QAction *toggleWindowAction ;
		QMenu *toolsMenu;
		QAction *exitAction;

		void createActions() ;
		void createMenus() ;


		// JK1 end

} ;
#endif

/* check connect signal - slot mechanism
/* check connect signal - slot mechanism
 * 2007-12-15 implementation part
*/

#include <QWidget>
#include <iostream>

#include "secondform.h"

SecondForm::SecondForm( QWidget *parent = 0 ) {
	setupUi( parent ) ;
	std::cout << "Constructor SecondForm" << std::endl ;

}
SecondForm::~SecondForm(){
}
#ifdef TRIAL_CODE

void SecondForm::on_secondPushButton_clicked() {
	std::cout << "Second Push Button clicked" << std::endl ;
}
#endif
// override "connectNotify" function of QObject ...
#ifdef FUTURE
	void SecondForm::connectNotify( const char* signal ) {

		std::cout << "Connection notification" << std::endl ;
	}
#endif

/* check connect signal - slot mechanism on stacked widgets
/* check connect signal - slot mechanism on stacked widgets
 * 2007-12-15 definition part
*/
#ifndef SECOND_FORM_H
#define SECOND_FORM_H
#include <QtGui>

#include "ui_secondform.h"

class SecondForm : public QWidget, public Ui::SecondForm
{
//	Q_OBJECT
	public:
		SecondForm( QWidget * );
		~SecondForm();
//		virtual void connectNotify( const char * signal ) ;
//	public slots:
//		void on_secondPushButton_clicked() ;
} ;
#endif


Attachment:

Attachment: secondform.ui
Description: application/designer

######################################################################
# Automatically generated by qmake (2.01a) Sa Dez 15 12:41:17 2007
######################################################################

CONFIG   += debug
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += firstform.h secondform.h mainwindow.h
FORMS += firstform.ui \
         secondform.ui
SOURCES += firstform.cpp main.cpp secondform.cpp mainwindow.cpp


Message 4 in thread

On 13.02.08 16:57:58, Jorge wrote:
> Andreas Pakulat schrieb:
> > On 07.02.08 22:33:01, Ionathan wrote:
> >   
> >> Hi group,
> >>
> >> I am playing around with stacked widgets the ultimate goal being to get
> >> the "connectSlotByName" - mechanism work.
> >>
> >> Now the attached code is a simple application where I use two - very
> >> similar - .ui files for stacking on a mainwindow-application.
> >>
> >> it shows the main widgets. however the .ui widgets do not show up ...
> >>
> >> What did I do wrong? The attached code compiles nicely and runs but ...
> >>     
> >
> > About everything. Sorry. I know you're probably just starting, but you
> > really need to go back to the start and read again the designer manual
> > and possibly also check some of the examples. 
> >
> > A few things that are wrong:
> >
> > - mainwindow inherits from both .ui files
> > - the 2 classes that use the .ui files, are both QDialogs and thus
> >   themselves top level windows, not embedded anywhere
> > - you create 2 empty QWidgets and put those into the stack, how can you
> >   expect them to show anything, except their background.
> >
> >   
> Thanks Andreas, I was a bit absent, now in the meantime I got something
> to work better, however it still does not switch the widgets ... If you
> would be so kind as to look over the attached files? should not be too
> much work ... tia Jorge

Two problems:

a) firstform.h: the class first subclasses from the ui-class, that won't
work. QObject subclasses always have to subclass from their
QObject-parent first (i.e. QWidget here)

b) your calling setupUi() with the parent that gets passed into your
widgets, which means all the ui from the .ui files gets done _onto_ that
parent widget - in your case the mainwindow. So everything actually
works correctly, just that the two widgets you add to the stacked widget
are simply empty.

Its also a good habit to call the paren class constructor, especially
QWidget to pass the parent parameter on to it so your QWidget subclass
ends up in the right parent (if any).

It still looks as if you'd want to re-read some of the basic chapters in
the Qt documentation, especially about the object model, layout
management and probably the designer manual.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

Andreas Pakulat schrieb:
> On 13.02.08 16:57:58, Jorge wrote:
>   
>> Andreas Pakulat schrieb:
>>     
>>> On 07.02.08 22:33:01, Ionathan wrote:
>>>   
>>>       
>>>> Hi group,
>>>>
>>>> I am playing around with stacked widgets the ultimate goal being to get
>>>> the "connectSlotByName" - mechanism work.
>>>>
>>>> Now the attached code is a simple application where I use two - very
>>>> similar - .ui files for stacking on a mainwindow-application.
>>>>
>>>> it shows the main widgets. however the .ui widgets do not show up ...
>>>>
>>>> What did I do wrong? The attached code compiles nicely and runs but ...
>>>>     
>>>>         
>>> About everything. Sorry. I know you're probably just starting, but you
>>> really need to go back to the start and read again the designer manual
>>> and possibly also check some of the examples. 
>>>
>>> A few things that are wrong:
>>>
>>> - mainwindow inherits from both .ui files
>>> - the 2 classes that use the .ui files, are both QDialogs and thus
>>>   themselves top level windows, not embedded anywhere
>>> - you create 2 empty QWidgets and put those into the stack, how can you
>>>   expect them to show anything, except their background.
>>>
>>>   
>>>       
>> Thanks Andreas, I was a bit absent, now in the meantime I got something
>> to work better, however it still does not switch the widgets ... If you
>> would be so kind as to look over the attached files? should not be too
>> much work ... tia Jorge
>>     
>
> Two problems:
>
> a) firstform.h: the class first subclasses from the ui-class, that won't
> work. QObject subclasses always have to subclass from their
> QObject-parent first (i.e. QWidget here)
>
> b) your calling setupUi() with the parent that gets passed into your
> widgets, which means all the ui from the .ui files gets done _onto_ that
> parent widget - in your case the mainwindow. So everything actually
> works correctly, just that the two widgets you add to the stacked widget
> are simply empty.
>
> Its also a good habit to call the paren class constructor, especially
> QWidget to pass the parent parameter on to it so your QWidget subclass
> ends up in the right parent (if any).
>
> It still looks as if you'd want to re-read some of the basic chapters in
> the Qt documentation, especially about the object model, layout
> management and probably the designer manual.
>
> Andreas
>
>   
Thanks Andreas - now it works and the slots in the ui-derived subclasses
(firstform.h and secondform.h) will be addressed from clicks in the
pushbutton widgets I added in the respective ui's.

The only thing which does not show up  is the background color I put to
the ui's (just for fun). But this is worth another thread.

Jörg

--
 [ signature omitted ] 

Message 6 in thread

Andreas Pakulat schrieb:
> On 15.02.08 12:27:20, Jorge wrote:
>   
>> But still you are right: I am coming from Qt3 and do that programming in
>> my sparetime -- so it is more or less a "hobby" ... and I am a freshman
>> to Qt4 - which is much more complex  ...
>>     
>  
> While there are certainly some differences between Qt3 and Qt4, this
> particular issue is not. You would've faced the same problem when
> simply replacing QStackedWidget with QWidgetStack and re-compiling with
> Qt3.
>
> Andreas
>
>   
tnx andreas ...

--
 [ signature omitted ]