Results 1 to 5 of 5

Thread: QWidget on QMainWindow

  1. #1

    Default QWidget on QMainWindow

    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

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

    Default Re: QWidget on QMainWindow

    Quote Originally Posted by jayreddy View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: QWidget on QMainWindow

    Qt Code:
    1. dialog::dialog(QWidget* parent): QWidget(parent)
    2. {
    3.  
    4. dialog= this;
    5. win= new QWidget(dialog);
    6. this->setGeometry(0, 0, 400, 100);
    7. win->setGeometry(0, 0, this->width(), this->height());
    8. grid= new QGridLayout;
    9. label= new QLabel("File gets deleted permanently.\n Do you wish to continue");
    10. label->setText("<font color=#000000>"+label->text()+"</font>" );
    11. button1= new QPushButton("OK");
    12. button2= new QPushButton("CANCEL");
    13. grid->addWidget(label, 0, 0, Qt::AlignCenter);
    14. grid->addWidget(button1, 1, 0, Qt::AlignLeft);
    15. grid->addWidget(button2, 1, 1, Qt::AlignRight);
    16. win->setLayout(grid);
    17. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. dilogbox= new dialog(QApplication::activeWindow());
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QApplication::activeWindow()->installEventFilter(dilogbox);
    To copy to clipboard, switch view to plain text mode 

    I need this as a part of mainwindow because I dont want a seperate window for dialogbox
    Can I know how to proceed
    Last edited by wysota; 5th January 2010 at 11:21. Reason: Missing [code] tags

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

    Default Re: QWidget on QMainWindow

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: QWidget on QMainWindow

    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.

    Qt Code:
    1. dilog->resize(QApplication::activeWindow()->width(), QApplication::activeWindow()->height());
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. void dialog:: paintEvent(QPaintEvent* event)
    2. {
    3. QPainter painter(this);
    4. QRect rect(0, 0, this->parentWidget()->width(), this->parentWidget()->height());
    5. painter.drawRect(rect);
    6. QColor color(Qt::black);
    7. color.setAlpha(100);
    8. painter.fillRect(rect, color);
    9. }
    10.  
    11.  
    12. bool dialog::eventFilter(QObject* obj, QEvent* event)
    13. {
    14.  
    15. if (event->type() == QEvent::Resize)
    16. {
    17. QResizeEvent *resize= static_cast<QResizeEvent*> (event);
    18. this->resize(resize->size());
    19. win->move(dialog->width()/2-win->width()/2, dialog->height()/2-win->height()/2);
    20. this->repaint();
    21. }
    22. if (event->type() == QEvent::Paint)
    23.  
    24. return false;
    25.  
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 
    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

Similar Threads

  1. switch a QWidget inside a QMainWindow to fullscreen
    By koenux in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2009, 22:25
  2. QMainWindow or QWidget start hidden?
    By marf in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2008, 18:23
  3. Resize QWidget in QMainWindow
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2007, 13:16
  4. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 10:03
  5. Replies: 1
    Last Post: 2nd May 2006, 22:11

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.