Results 1 to 4 of 4

Thread: Help me about QGridLayout and ScrollArea

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Help me about QGridLayout and ScrollArea

    Please help me this problem.
    I using QGridLayout and array of QToolButton to display a array of images (Image is the icon of button). But the button (image) overlap...
    I want to display the array button in 3 columns....
    help me!
    Thanks!
    this is my code

    Qt Code:
    1. QDir currentImageDir(LibPath+"/"+dir);
    2. currentImageDir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
    3. QStringList ImageEntries = currentImageDir.entryList();
    4.  
    5. singalImageMapper = new QSignalMapper(this);
    6. QGridLayout* gridEPG;
    7. QScrollArea *scrollArea = new QScrollArea();
    8. QWidget *contentsWidget = new QWidget(scrollArea);
    9. gridEPG = new QGridLayout(contentsWidget);
    10.  
    11. scrollArea->setWidgetResizable(true);
    12. scrollArea->setWidget(contentsWidget);
    13. contentsWidget->setLayout(gridEPG);
    14. contentsWidget->setMinimumSize(scrollArea->width(), scrollArea->height());
    15.  
    16. ui->imageLayout->addWidget(scrollArea, 1, 0, 2, 3);
    17. QString filename[ImageEntries.count()];
    18.  
    19. for (int x = 0; x < ImageEntries.count(); x++)
    20. {
    21. // int row = x;
    22. //int col = i%3;
    23. filename[x]= ImageEntries.at(x);
    24. QToolButton* button = new QToolButton();
    25. button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    26. button->setIcon(QIcon(LibPath + "/" + dir + "/" + filename[x]));
    27. button->setFixedSize(QSize(96,76));
    28. button->setIconSize(QSize(96,76));
    29. connect(button, SIGNAL(clicked()), singalImageMapper, SLOT(map()));
    30. singalImageMapper->setMapping(button, filename[x]);
    31. for(int i=0; i<3; i++)
    32. gridEPG->addWidget(button, x, i%3);
    33.  
    34. }
    35. ui->fileImgUsing->setText(QString(imgPath + "/" + dir));
    36. connect(singalImageMapper, SIGNAL(mapped(QString)),this, SIGNAL(clicked(QString)));
    37. connect(this, SIGNAL(clicked(QString)),this, SLOT(onButtonClicked(QString)));
    To copy to clipboard, switch view to plain text mode 

    and my result

    error.png

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help me about QGridLayout and ScrollArea

    [QUOTE=homerux;248996]
    Qt Code:
    1. for(int i=0; i<3; i++)
    2. gridEPG->addWidget(button, x, i%3);
    To copy to clipboard, switch view to plain text mode 

    You are trying to put the same button object in each of three columns? If that is what you intend then you need to duplicate the button for each column (i.e. three different objects with the same image). If that is not what you intended then remove the for loop and change "i % 3" to " x % 3".

    Set the sizeConstraint() of the layout in the scroll area contents widget to QLayout::SetFixedSize and it should adjust the size of the contentsWidget to match the fixed size of the contained tool buttons.

  3. The following user says thank you to ChrisW67 for this useful post:

    homerux (11th August 2013)

  4. #3
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Help me about QGridLayout and ScrollArea

    [QUOTE=ChrisW67;249013]
    Quote Originally Posted by homerux View Post
    Qt Code:
    1. for(int i=0; i<3; i++)
    2. gridEPG->addWidget(button, x, i%3);
    To copy to clipboard, switch view to plain text mode 

    You are trying to put the same button object in each of three columns? If that is what you intend then you need to duplicate the button for each column (i.e. three different objects with the same image). If that is not what you intended then remove the for loop and change "i % 3" to " x % 3".

    Set the sizeConstraint() of the layout in the scroll area contents widget to QLayout::SetFixedSize and it should adjust the size of the contentsWidget to match the fixed size of the contained tool buttons.
    I want put a array of button in 3 columns in a scroll layout, but it not work. Can you help me?

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help me about QGridLayout and ScrollArea

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Window: public QScrollArea
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit Window(QWidget *p = 0): QScrollArea(p) {
    8. QWidget *contents = new QWidget(this);
    9.  
    10. QGridLayout *layout = new QGridLayout(this);
    11. for (int n = 0; n < 150; ++n) {
    12. QToolButton *b = new QToolButton(this);
    13. b->setIcon(qApp->style()->standardIcon(QStyle::SP_FileIcon));
    14. layout->addWidget(b, n / 3, n % 3);
    15. }
    16. contents->setLayout(layout);
    17.  
    18. setWidget(contents);
    19. }
    20. };
    21.  
    22. int main(int argc, char **argv)
    23. {
    24. QApplication app(argc, argv);
    25. Window w;
    26. w.resize(200, 200);
    27. w.show();
    28. return app.exec();
    29. }
    30. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    homerux (12th August 2013)

Similar Threads

  1. Stucked on ScrollArea
    By Asus_G72GX in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2013, 12:12
  2. scrollArea
    By skizzik in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2011, 12:55
  3. ItemViews in ScrollArea
    By SElsner in forum Newbie
    Replies: 3
    Last Post: 4th June 2010, 23:59
  4. Autoscroll in ScrollArea
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2009, 05:37
  5. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01

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.