PDA

View Full Version : QMainWindow and custom widget doesn't show



Peppy
23rd December 2009, 21:23
I have problems with this code:
(MainWindow.cpp)


MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
{
QWidget * widget = new QWidget;
QVBoxLayout * layout = new QVBoxLayout;
MyWidget * w = new MyWidget;
w->setRectBackgroundColor(255,255,255,255);
w->setHeight(110);
w->show();
layout->addWidget(w,0);
widget->setLayout(layout);
setCentralWidget(widget);
}

(MyWidget.h)


class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget(void){ };
void setHeight(int height);
void setRectBackgroundColor(int r,int g, int b, int a);
void setRectBackgroundColor(const QColor &bgColor);
protected:
virtual void paintEvent(QPaintEvent * e);
private:
QColor rectBgColor;
int rectHeight;
};

(MyWidget.cpp)


MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
void MyWidget::setRectBackgroundColor(const QColor &bgColor)
{
rectBgColor = bgColor;
}
void MyWidget::setRectBackgroundColor(int r, int g, int b, int a)
{
rectBgColor = QColor(r,g,b,a);
}
void MyWidget::setHeight(int height)
{
rectHeight = height;
}
void MyWidget::paintEvent(QPaintEvent *e)
{
QPainter painter;
painter.begin(this);
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(rectBgColor,Qt::SolidPatte rn));
painter.drawRect(QRect(0,0,rect().width()+1,rectHe ight));
painter.end();
}


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. :(

kiss-o-matic
24th December 2009, 08:12
How are you adding your widget to the QMainWindow, and how are you calling the main window? Basically, show your main().

Peppy
24th December 2009, 16:58
main.cpp:


int main(int argv, char * argc[])
{
QApplication a(argv,argc);
MainWindow mW;
mW.show();
return a.exec();
}

numbat
25th December 2009, 11:11
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.

Peppy
25th December 2009, 15:21
Done, still nothing...

soxs060389
25th December 2009, 18:09
Did you try to add other custom widgets to your MainWindow as central widget? Was that successfull?

Peppy
25th December 2009, 20:47
No, I didn't.

numbat
26th December 2009, 10:27
Done, still nothing...

Works for me with your code. Maybe you could upload a zip of your project. My implementation of sizeHint follows:


QSize MyWidget::sizeHint() const
{
return QSize(100, 110);
}

LoginFailed
26th December 2009, 13:40
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.

Peppy
26th December 2009, 16:09
my sizeHint():


QSize MyWidget::sizeHint()
{
return QSize(rect().width()+1, rectHeight );
}