Results 1 to 14 of 14

Thread: QStackWidget example

  1. #1
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation QStackWidget example

    I am trying to build a desktop app.

    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

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    I dont get what is so difficult/different.

    QWidget *firstPageWidget = new QWidget;
    QWidget *secondPageWidget = new QWidget;
    QWidget *thirdPageWidget = new QWidget;

    QStackedWidget *stackedWidget = new QStackedWidget;
    stackedWidget->addWidget(firstPageWidget);
    stackedWidget->addWidget(secondPageWidget);
    stackedWidget->addWidget(thirdPageWidget);
    all you have to do is read a file, generate the widgets, add them to the stackedwidget.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    Yes, i have done this .. But I don't know how to generate the widgets on the fly ? i.e every page should have a widget. so in what I have done. I have put the things into stackwidget before, i.e i create page widgets and then put them into the stackwidget.

    but i need to know how to generate widgets on the fly when i read the file and create so many widgets as many pages I have.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStackWidget example

    You realize C++ has loop constructs, right?

  5. #5
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    you are not understanding..its not about how to code.. I know that ! and its no use being sarcastic.
    its about how it is to be done..
    I haven't got the clear idea of flow.. if I generate widgets, I give the name of the widget for each page like page1widget ...and so on etc..

    But how do I do that on the fly ? i.e as soon as I read the file, I should be able to generate the widgets. that was what I was looking for..

    anyway thanks for the "loop constructs" !

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    "i.e as soon as I read the file, I should be able to generate the widgets. that was what I was looking for.."

    1 - Read file.
    2 - Make widgets.


    Where are you stuck?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStackWidget example

    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:

    Qt Code:
    1. void PageView::onDocumentChanged( Document * pDoc )
    2. {
    3. // Delete all of the old page widgets first
    4. foreach( QWidget * pWidget, pageImages )
    5. {
    6. stackedWidget->removeWidget( pWidget ); // only removes the widget, doesn't delete it
    7. pWidget->deleteLater(); // but this will
    8. }
    9.  
    10. // and empty the vector of the now deleted pointers
    11. pageImages.clear();
    12.  
    13. // now create new page widgets for every page in the document
    14. if ( pDoc )
    15. {
    16. int nPages = pDoc->pageCount();
    17. for ( int nPage = 0; nPage < nPages; ++nPage )
    18. {
    19. PageImageWidget * pPageImage = new PageImageWidget(); // or whatever class it is you use to display the page images
    20. pPageImage->setImage( pDoc->generatePageImage( nPage ) ); // making this up - you have to write this
    21. stackedWidget->addWidget( pPageImage ); // add the new widget to the stack
    22. pageImages.push_back( pPageImage ); // and remember it in the vector
    23. }
    24. }
    25.  
    26. stackedWidget->setCurrentIndex( 0 ); // display the first page
    27. }
    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.

  8. #8
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    First of all sorry for sub-topic ing and posting it again..Will keep that in mind..

    Thanks. Will check on this and try to implement this way.. will try both the ways with stackwidget and without.. but as said before, making widgets for each page seems not a good option, so will try the approach but without stackwidget.

    thanks a lot.

    I do believe what you said about creating individual widgets for each page is not a right option, so what would you suggest instead of that ?
    make use of signals and slots and do something like this ?

    - thumbnails (qlistwidget)
    - content (qgraphicsview)

    keep signals and slots to interact and paint each page when a page is requested. and keep few thumbnails and content in cache, say maybe 10 of them ?

    would this be a good idea instead of the non-efficient stackwidget idea..

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStackWidget example

    - thumbnails (qlistwidget)
    - content (qgraphicsview)
    QListWidget might be suitable, but if you really want to emulate Acrobat Reader behavior, I would use a QTableWidget instead, so if the user expanded the thumbnail sidebar to make it wider, you could reorganize the thumbnails into two or more columns. QListWidget would give you only one column.

    You could use QGraphicsView for the content display with the page implemented as a QGraphicsPixmapItem. It would give the ability to zoom the image easily, but I am not sure the appearance would be very appealing. If you use a QTextEdit instead, you could easily scale the page simply by changing the font size, and get a very accurate representation (as opposed to a pixmap, which could eventually be zoomed to the point where it becomes pixelated - whereas fonts won't).

    But since you are already parsing the document content when you read it in, you know what's in it. Why throw that away by converting the text to a picture of text? If you keep it as text and display it in a QTextEdit, you give the user (and yourself) the ability to search for and highlight phrases, copy text for pasting elsewhere, and other text-related operations. In particular, selecting text is pretty hard to do when the user interacts with an image - you have to go backwards from mouse coordinates in pixels to the mapping of text to those same pixels in the image. QTextEdit gives that to you for free - you just have to listen for the right signals.

    For thumbnails, you need as many as you have visible cells in the thumbnail viewer. When the table scrolls, you shift the images from cell to cell as appropriate, generating new pixmaps when needed and throwing away old ones that are no longer visible. Alternatively, you could keep an array of pixmap pointers, one for every page in the document, generate them as needed (i.e. when the pages become visible in the thumbnail browser), and never throw them away until the document is closed. Since the thumbnails are pretty small, you could probably keep the entire document's worth without memory issues.

  10. #10
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    thats nice..will use those widgets suggested.
    but i wouldn't use Qtextwidget as my content is binary data not in text format. it comes in different formats something like unipen etc..so basically i will be storing the data in QLists etc.. so in this case I wouldn't be able to use Qtextwidget right ?

    so I have to use Qgraphicspixmapitem or similar and as Qgraphicsview and Qgraphicsscene already have option of makiing things zoom and editable, I could make use of them ? am i right ?

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStackWidget example

    In your original post you said:

    I want to do a multi-file format reader..I have the parsers done for few of the file formats that I need. But I want to make a GUI like Adobe Acrobat Reader.
    i.e show thumbnails of the files on the left sidebar and content on the right.
    So what do you mean when you say you have "parsers"? It is irrelevant whether the files you read are binary. What are you converting their content to when you "parse" them?

  12. #12
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    I am converting to simple xy coord and storing in a qlist

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStackWidget example

    I am converting to simple xy coord and storing in a qlist
    What are you converting to "simple xy coord"? The text in the PDF or other format file? How do you convert text into x-y coordinates?

    Give me an example: If the file contained the text "I want to do a multi-file format reader", how do you turn that string into an x-y pair?

  14. #14
    Join Date
    Jun 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStackWidget example

    the data which is in the files itself is a x-y pair , i don't make it, i only read the data from files and and store it.

Similar Threads

  1. How to set spinner image for qstackwidget ???
    By naufalahad in forum Qt Programming
    Replies: 11
    Last Post: 22nd December 2011, 02:39
  2. QTreeWidget and QStackWidget
    By Paulo Fernando Pimenta in forum Newbie
    Replies: 3
    Last Post: 1st March 2011, 18:27
  3. ShowFullScreen on QStackWidget don't work
    By Ratheendrans in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2010, 18:34
  4. QStackWidget layoutspacing
    By killerwookie99 in forum Qt Programming
    Replies: 2
    Last Post: 14th August 2008, 21:38
  5. Reuse a Qwidget with a QStackWidget
    By ucomesdag in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2007, 02:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.