Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31

Thread: QScrollArea With Custom Widgets

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea With Custom Widgets

    Works quite fine for me.
    Attached Files Attached Files

  2. #22
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea With Custom Widgets

    Take a look, whenever I create this class, I changed your class in a very SIMPLE fashion. Logically thinking this should work fine, just a bunch of widgets just as you did, but how does your code magically work but these widgets magically not work?

    See attachment ... thx we're getting close.
    Attached Files Attached Files

  3. #23
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea With Custom Widgets

    The reason is simple and I can tell you just by looking at your code - you didn't use layouts and you didn't reimplement sizeHint(). At least one of the two has to be used for layouts to work. Otherwise QWidget::sizeHint() will be used and I'm sure it doesn't return a reasonable size for your widget.

  4. #24
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea With Custom Widgets

    Wrong, thats not the problem because I put some gridlayouts and did some sizeHint reimplementing, and nothing... can you please try and compile mine the way you believe works?

  5. #25
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea With Custom Widgets

    Looking at your code, I see one big problem:

    Complex widget does not have a layout to handle it's size, that is why you don't see anything ( at least I don't ). So after creating the widgets inside it, you should use setFixedSize( cumulatedSize ), where cumulatedSize is the total size of the widgets inside Complex ( I used 100, 100 and it worked, but probably the size is smaller ).

    Also, to see any of the widgets whatsoever, create the widgets in complex with "this" as parent... Not having a layout makes them not having any parent, therefore no one to forward them paint events( actually this is more important than the 1st problem )

    I hope you agree.

    regards

  6. #26
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea With Custom Widgets

    I did the following modification ( at the end of the Complex constructor )

    Qt Code:
    1. #include <QtGui>
    2. #include "flowlayout.h"
    3. #include <QResizeEvent>
    4.  
    5. class Complex : public QWidget {
    6. public:
    7. QPushButton *Host;
    8. QPushButton *Info;
    9. QLabel *qlGameNum;
    10. QLabel *qlGameTitle;
    11. QLabel *qlPlayerNum;
    12. QLabel *qlIcons;
    13. Complex(int i, QWidget *parent=0): QWidget(parent){
    14. /*QHBoxLayout *l = new QHBoxLayout(this);
    15.   l->addWidget(new QPushButton(QString("Button %1").arg(i)));
    16.   QComboBox *cb = new QComboBox;
    17.   cb->addItems(QStringList() << QString::number(i));
    18.   cb->setMinimumWidth(50);
    19.   l->addWidget(cb);*/
    20. Host = new QPushButton( this ) ;
    21. Host->setObjectName(QString::fromUtf8("Host"));
    22. Host->setGeometry(QRect(20, 50, 61, 41));
    23. Info = new QPushButton( this );
    24. Info->setObjectName(QString::fromUtf8("Info"));
    25. Info->setGeometry(QRect(80, 60, 31, 21));
    26. qlGameNum = new QLabel( this );
    27. qlGameNum->setObjectName(QString::fromUtf8("qlGameNum"));
    28. qlGameNum->setGeometry(QRect(10, 0, 46, 14));
    29. qlGameTitle = new QLabel(this);
    30. qlGameTitle->setObjectName(QString::fromUtf8("qlGameTitle"));
    31. qlGameTitle->setGeometry(QRect(10, 20, 61, 16));
    32. qlPlayerNum = new QLabel(this);
    33. qlPlayerNum->setObjectName(QString::fromUtf8("qlPlayerNum"));
    34. qlPlayerNum->setGeometry(QRect(130, 0, 21, 16));
    35. qlIcons = new QLabel(this);
    36. qlIcons->setObjectName(QString::fromUtf8("qlIcons"));
    37. qlIcons->setGeometry(QRect(65, 0, 61, 20));
    38. Host->setText(QApplication::translate("Form", "Host", 0, QApplication::UnicodeUTF8));
    39. Info->setText(QApplication::translate("Form", "Info", 0, QApplication::UnicodeUTF8));
    40. qlGameNum->setText(QApplication::translate("Form", "Game 1", 0, QApplication::UnicodeUTF8));
    41. qlGameTitle->setText(QApplication::translate("Form", "Game Title", 0, QApplication::UnicodeUTF8));
    42. qlPlayerNum->setText(QApplication::translate("Form", "0/8", 0, QApplication::UnicodeUTF8));
    43. setFixedSize( 200, 100 );
    44. }
    45. };
    46.  
    47. class Widget : public QWidget {
    48. public:
    49. Widget(QWidget *parent = 0) : QWidget(parent){}
    50. bool eventFilter(QObject *o, QEvent *e){
    51. if(e->type()!=QEvent::Resize) return false;
    52. int w = ev->size().width()-20; // cheating here
    53. setFixedSize(w, layout()->heightForWidth(w));
    54. return false;
    55. }
    56. };
    57.  
    58. int main(int argc, char **argv){
    59. QApplication app(argc, argv);
    60. QWidget *wgt = new Widget;
    61. FlowLayout *l = new FlowLayout(wgt);
    62. for(int i=0;i<20;i++)
    63. l->addWidget(new Complex(i+1, wgt));
    64. wgt->setMinimumSize( wgt->sizeHint() );
    65. area.setWidget(wgt);
    66. area.installEventFilter(wgt);
    67. area.show();
    68. return app.exec();
    69. }
    To copy to clipboard, switch view to plain text mode 

    I also attached a screenshot of what I obtained.

    Regards
    Attached Images Attached Images

  7. The following user says thank you to marcel for this useful post:

    VireX (22nd April 2007)

  8. #27
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea With Custom Widgets

    My suggestion:
    Use a layout in Complex too, because it is not recommended to place widgets manually within a parent widget. After all, that is what layouts are for.

    Placing widgets manually is tedious, and as the number of widgets increases the code gets hard to maintain and to read and also this leads to a lot of useless coordinate computations. You should let a layout do this for you.

    Regards

  9. #28
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea With Custom Widgets

    Quote Originally Posted by VireX View Post
    Wrong, thats not the problem because I put some gridlayouts and did some sizeHint reimplementing, and nothing... can you please try and compile mine the way you believe works?
    I don't know what "some" is, but I'm sure the problem is with the size hint. What marcel said just proves that.

  10. #29
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea With Custom Widgets

    I don't know what "some" is, but I'm sure the problem is with the size hint. What marcel said just proves that.
    Yes, with the sizeHint exactly...
    There is no one to compute the size hint for the Complex widget... It should be set manually.

    Or Virex should use layouts...

    Regards

  11. The following user says thank you to marcel for this useful post:

    VireX (22nd April 2007)

  12. #30
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea With Custom Widgets

    Quote Originally Posted by marcel View Post
    Yes, with the sizeHint exactly...
    There is no one to compute the size hint for the Complex widget... It should be set manually.

    Or Virex should use layouts...
    This is exactly what I said two posts ago

  13. The following user says thank you to wysota for this useful post:

    VireX (22nd April 2007)

  14. #31
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea With Custom Widgets

    THanks, adding setLayout (forgot I didn't declare the gridlayout as parent so i didn't setLAyout) and setFixedSize did the trick.

Similar Threads

  1. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 03:38
  2. create custom widgets
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 20th November 2006, 08:24
  3. Global includes with designer custom widgets
    By mab in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2006, 22:06
  4. size issues for custom QWidget in QScrollArea
    By anotheruser in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2006, 14:52
  5. container populated with custom widgets
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 16th April 2006, 21:02

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.