I just need to know how to put widgets onto QStackWidget and display one by one ? just a simple example would be good enough. and if i load a file and then generate pages after parsing it, how do I add the pages dynamically to the QStackWidget ? in the example in the documentation they show creating pages and then inserting them onto QStackWidget.
So I need some help ..some inputs would be helpful..Thanks
Don't start a new thread asking the same question you asked in a previous thread. It isn't polite, especially when people are still answering the questions in the first posting.
Although I strongly think that your approach of creating a widget for every page in your document is the wrong way to do what you are trying to do, if you want to persist in going down this road by driving backwards, here's what you need to do:
- In the constructor for your main display view (call it the "PageView" class), you create a QStackedWidget. Nothing more. Do not add pages to it, nothing. Hold it in a member variable, call it "stackedWidget".
- PageView class needs a member variable QVector< QWidget * > that holds the widget pointers for each page. Call it "pageImages"
- PageView needs a slot, something that tells it the document you want to display has changed. Call it "onDocumentChanged". Maybe it also passes a pointer to some kind of Document object, that can tell the PageView how many pages there are in the document.
- In this slot, the simplest, most brute force way to do things is to 1) remove all of the child widgets from the QStackedWidget (if any) and destroy them, and 2) add a new child widget to the stack for each page in the Document and put the image in it. The pseudocode to do this would look something like this:
void PageView::onDocumentChanged( Document * pDoc )
{
// Delete all of the old page widgets first
foreach
( QWidget * pWidget, pageImages
) {
stackedWidget->removeWidget( pWidget ); // only removes the widget, doesn't delete it
pWidget->deleteLater(); // but this will
}
// and empty the vector of the now deleted pointers
pageImages.clear();
// now create new page widgets for every page in the document
if ( pDoc )
{
int nPages = pDoc->pageCount();
for ( int nPage = 0; nPage < nPages; ++nPage )
{
PageImageWidget * pPageImage = new PageImageWidget(); // or whatever class it is you use to display the page images
pPageImage->setImage( pDoc->generatePageImage( nPage ) ); // making this up - you have to write this
stackedWidget->addWidget( pPageImage ); // add the new widget to the stack
pageImages.push_back( pPageImage ); // and remember it in the vector
}
}
stackedWidget->setCurrentIndex( 0 ); // display the first page
}
void PageView::onDocumentChanged( Document * pDoc )
{
// Delete all of the old page widgets first
foreach( QWidget * pWidget, pageImages )
{
stackedWidget->removeWidget( pWidget ); // only removes the widget, doesn't delete it
pWidget->deleteLater(); // but this will
}
// and empty the vector of the now deleted pointers
pageImages.clear();
// now create new page widgets for every page in the document
if ( pDoc )
{
int nPages = pDoc->pageCount();
for ( int nPage = 0; nPage < nPages; ++nPage )
{
PageImageWidget * pPageImage = new PageImageWidget(); // or whatever class it is you use to display the page images
pPageImage->setImage( pDoc->generatePageImage( nPage ) ); // making this up - you have to write this
stackedWidget->addWidget( pPageImage ); // add the new widget to the stack
pageImages.push_back( pPageImage ); // and remember it in the vector
}
}
stackedWidget->setCurrentIndex( 0 ); // display the first page
}
To copy to clipboard, switch view to plain text mode
You could rely on the QObject::children() method to store and return the list of child widgets in the stack, but I find it simpler to maintain a separate list in the QVector. That way, I can go directly to a specific child widget without having to retrieve the whle list first from the stack.
Bookmarks