Results 1 to 2 of 2

Thread: FlowLayout for array of pushbuttons with scrolling feature

  1. #1
    Join Date
    Jul 2021
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Thumbs up FlowLayout for array of pushbuttons with scrolling feature

    Hi All,

    I am trying to achieve something like the following:
    flow_Layout_QPushButtons.jpg

    Here's my code for it.

    Qt Code:
    1. Window::Window()
    2. {
    3. QMainWindow *a= new QMainWindow(this);
    4.  
    5. QWidget *flowWidget = new QWidget();
    6. FlowLayout *flowLayout = new FlowLayout();
    7. flowWidget->setLayout(flowLayout);
    8. int n=20;
    9. QVector <QPushButton *> buttons(n);
    10.  
    11. for (int ii=0;ii<n;ii++)
    12. {
    13. QPushButton * pb = new QPushButton(); // creating buttons
    14.  
    15. pb->setMinimumSize(200,200);
    16. buttons.push_back(pb); // adding buttons to qvector
    17.  
    18. flowLayout->addWidget(pb);
    19.  
    20. }
    21.  
    22. QScrollArea *scroll =new QScrollArea();
    23. scroll->setWidget(flowWidget);
    24.  
    25. a->setCentralWidget(scroll);
    26.  
    27.  
    28. a->show();
    29.  
    30. setWindowTitle(tr("Flow Layout"));
    31. }
    To copy to clipboard, switch view to plain text mode 


    It seems like flowLayout is not working after the use of scrollArea widget. I want the content of window-2 to be displayed within window-1.
    Please suggest how to achieve it?

    Thanks!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,102
    Thanks
    289
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: FlowLayout for array of pushbuttons with scrolling feature

    Seems to me you are constructing your GUI inside out (or backwards, depending on your point of view). Usually, the QMainWindow is the topmost widget in an application and other widgets live inside it as children. In your code, "Window" (whatever that is) is the parent of the QMainWindow. Looks like you are hacking away at the FlowLayout example code without really understanding it.

    Forget this "Window" class. In the FlowLayout example, this class was used simply to hold the layout and functioned as the top-level application widget. In your case, you should derive your own MainWindow class from QMainWindow if you want all of the bells and whistles (menus, toolbars, status bars, etc) of a QMainWindow.

    In your MainWindow constructor, create a QScrollArea and make it the central widget. Create a QWidget and set it as the widget controlled by QScrollArea (QScrollArea::setWidget()). Be sure you also set the QScrollWidget::setWidgetResizable() flag to true. Create the FlowLayout and add it to the QWidget. Create your push buttons and add them to the flow layout.

    In main(), create the QApplication, create the MainWindow, call show() on the MainWindow instance, then exec() on the app.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. flowlayout label spacing issue
    By SElsner in forum Qt Programming
    Replies: 4
    Last Post: 12th August 2014, 11:05
  2. Replies: 2
    Last Post: 30th January 2013, 15:20
  3. Qwt Zoom through PushButtons?
    By sonulohani in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2012, 10:56
  4. Replies: 6
    Last Post: 13th August 2011, 18:31
  5. Three pushButtons in a shape of one
    By Kenji_Takahashi in forum Qt Programming
    Replies: 13
    Last Post: 7th September 2009, 19:25

Tags for this Thread

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.