PDA

View Full Version : QWidget on QMainWindow



jayreddy
5th January 2010, 09:18
Hii all.. I have placed my widget which contains a gridlayout with 2 pushbuttons on a mainwindow. I tried to set background color to my widget but only the children of it say the 2 buttons are changing to specified color but not the whole widget. Can anyone give me the reason.

Also I want this widget to accept all events happening to mainwindow until it is explicitly closed or click the cancel button which is on the widget. During this can i make the background of widget that is remaining part of mainwindow to some other color. No events should happen to other widgets on the mainwindow when this widget is visible on mainwindow.

I tried with QDialog with setmodal property and it block all events to mainwindow until it is closed but this dialog comes as a seperate window but not as a part of mainwindow. I want this widget as a part of mainwindow and accept all events happening to the mainwindow disabling the other widgets on the mainwindow.


..

Thanks
Jay

wysota
5th January 2010, 09:41
Hii all.. I have placed my widget which contains a gridlayout with 2 pushbuttons on a mainwindow. I tried to set background color to my widget but only the children of it say the 2 buttons are changing to specified color but not the whole widget. Can anyone give me the reason.
Can we see the code?


Also I want this widget to accept all events happening to mainwindow until it is explicitly closed or click the cancel button which is on the widget. During this can i make the background of widget that is remaining part of mainwindow to some other color. No events should happen to other widgets on the mainwindow when this widget is visible on mainwindow.
You can install an event filter to intercept events although it seems to me you should just make your widget a dialog modal to the main window.


I tried with QDialog with setmodal property and it block all events to mainwindow until it is closed but this dialog comes as a seperate window but not as a part of mainwindow. I want this widget as a part of mainwindow and accept all events happening to the mainwindow disabling the other widgets on the mainwindow.
Forcing the widget to be part of the main window is somewhat... strange. Why do you need that?

jayreddy
5th January 2010, 09:50
dialog::dialog(QWidget* parent): QWidget(parent)
{

dialog= this;
win= new QWidget(dialog);
this->setGeometry(0, 0, 400, 100);
win->setGeometry(0, 0, this->width(), this->height());
grid= new QGridLayout;
label= new QLabel("File gets deleted permanently.\n Do you wish to continue");
label->setText("<font color=#000000>"+label->text()+"</font>" );
button1= new QPushButton("OK");
button2= new QPushButton("CANCEL");
grid->addWidget(label, 0, 0, Qt::AlignCenter);
grid->addWidget(button1, 1, 0, Qt::AlignLeft);
grid->addWidget(button2, 1, 1, Qt::AlignRight);
win->setLayout(grid);
}

this is how I created my widget and I placed this on main application i.e mainwindow


dilogbox= new dialog(QApplication::activeWindow());

I tried this to install all events of mainwindow onto dialogbox but it didn't work :(


QApplication::activeWindow()->installEventFilter(dilogbox);

I need this as a part of mainwindow because I dont want a seperate window for dialogbox
Can I know how to proceed

wysota
5th January 2010, 10:24
And where do you set the background color?

Your widget is really a good candidate for a dialog... You can remove its decorations by passing an appropriate hint to the window manager to make it look more like it was embedded into the parent window.

How does your eventFilter() implementation look like?

jayreddy
6th January 2010, 08:27
I have made an empty widget that is of my application size placing on my application as a child and in that empty widget I have placed my dialog widget(now my dialog widget is win) in the center and made the widget transparent so that I can block events to background application.


dilog->resize(QApplication::activeWindow()->width(), QApplication::activeWindow()->height());

Now I tried to paint my background application with black color. Initially it was fine but when application is resized then it is not painting the latest resized part of my application although my empty widget is being increased with my application size.


void dialog:: paintEvent(QPaintEvent* event)
{
QPainter painter(this);
QRect rect(0, 0, this->parentWidget()->width(), this->parentWidget()->height());
painter.drawRect(rect);
QColor color(Qt::black);
color.setAlpha(100);
painter.fillRect(rect, color);
}


bool dialog::eventFilter(QObject* obj, QEvent* event)
{

if (event->type() == QEvent::Resize)
{
QResizeEvent *resize= static_cast<QResizeEvent*> (event);
this->resize(resize->size());
win->move(dialog->width()/2-win->width()/2, dialog->height()/2-win->height()/2);
this->repaint();
}
if (event->type() == QEvent::Paint)

return false;

return true;
}

May be this is due to my paintevent hasn't able to fetch the latest application size . How can I get my application size for every resize event so that I can paint the background