Results 1 to 10 of 10

Thread: QMainWindow and custom widget doesn't show

  1. #1
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Unhappy QMainWindow and custom widget doesn't show

    I have problems with this code:
    (MainWindow.cpp)
    Qt Code:
    1. MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
    2. {
    3. QWidget * widget = new QWidget;
    4. QVBoxLayout * layout = new QVBoxLayout;
    5. MyWidget * w = new MyWidget;
    6. w->setRectBackgroundColor(255,255,255,255);
    7. w->setHeight(110);
    8. w->show();
    9. layout->addWidget(w,0);
    10. widget->setLayout(layout);
    11. setCentralWidget(widget);
    12. }
    To copy to clipboard, switch view to plain text mode 
    (MyWidget.h)
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MyWidget(QWidget *parent = 0);
    6. ~MyWidget(void){ };
    7. void setHeight(int height);
    8. void setRectBackgroundColor(int r,int g, int b, int a);
    9. void setRectBackgroundColor(const QColor &bgColor);
    10. protected:
    11. virtual void paintEvent(QPaintEvent * e);
    12. private:
    13. QColor rectBgColor;
    14. int rectHeight;
    15. };
    To copy to clipboard, switch view to plain text mode 
    (MyWidget.cpp)
    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    2. {
    3. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    4. }
    5. void MyWidget::setRectBackgroundColor(const QColor &bgColor)
    6. {
    7. rectBgColor = bgColor;
    8. }
    9. void MyWidget::setRectBackgroundColor(int r, int g, int b, int a)
    10. {
    11. rectBgColor = QColor(r,g,b,a);
    12. }
    13. void MyWidget::setHeight(int height)
    14. {
    15. rectHeight = height;
    16. }
    17. void MyWidget::paintEvent(QPaintEvent *e)
    18. {
    19. QPainter painter;
    20. painter.begin(this);
    21. painter.setPen(Qt::NoPen);
    22. painter.setBrush(QBrush(rectBgColor,Qt::SolidPattern));
    23. painter.drawRect(QRect(0,0,rect().width()+1,rectHeight));
    24. painter.end();
    25. }
    To copy to clipboard, switch view to plain text mode 

    Problem is: In main window it doesn't show any white rectangle... I think is problem with body of MainWindow constructor, because on QWidget window it is working. I am so unhappy with this problem.

  2. #2
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    3

    Default Re: QMainWindow and custom widget doesn't show

    How are you adding your widget to the QMainWindow, and how are you calling the main window? Basically, show your main().

  3. #3
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow and custom widget doesn't show

    main.cpp:
    Qt Code:
    1. int main(int argv, char * argc[])
    2. {
    3. QApplication a(argv,argc);
    4. MainWindow mW;
    5. mW.show();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMainWindow and custom widget doesn't show

    The documentation says:
    When implementing a new widget, it is almost always useful to reimplement sizeHint() to provide a reasonable default size for the widget and to set the correct size policy with setSizePolicy().

    By default, composite widgets which do not provide a size hint will be sized according to the space requirements of their child widgets.
    and
    QSizePolicy::Fixed - The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button).
    In short, you need to provide a sizeHint implementation.

  5. #5
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow and custom widget doesn't show

    Done, still nothing...

  6. #6
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMainWindow and custom widget doesn't show

    Did you try to add other custom widgets to your MainWindow as central widget? Was that successfull?

  7. #7
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow and custom widget doesn't show

    No, I didn't.

  8. #8
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMainWindow and custom widget doesn't show

    Quote Originally Posted by Peppy View Post
    Done, still nothing...
    Works for me with your code. Maybe you could upload a zip of your project. My implementation of sizeHint follows:
    Qt Code:
    1. QSize MyWidget::sizeHint() const
    2. {
    3. return QSize(100, 110);
    4. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Dec 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QMainWindow and custom widget doesn't show

    Cause the MainWindow has its own Layout and in its center has a Widget.I think first you should use the function setCenterWidget() to add an Widget as you defined(like QGroupBox),and setLayout in this Widget , then set Widgets which you want to show in you MainWindow in this Layout.

  10. #10
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow and custom widget doesn't show

    my sizeHint():
    Qt Code:
    1. QSize MyWidget::sizeHint()
    2. {
    3. return QSize(rect().width()+1, rectHeight );
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 10th December 2009, 22:53
  2. Problem building application with custom widget
    By Uthar in forum Installation and Deployment
    Replies: 6
    Last Post: 19th January 2009, 17:57
  3. How to show custom widget in TreeView's cell :-/
    By WolfMM in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2007, 11:16
  4. paint central widget of a QMainWindow???
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 08: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.