Results 1 to 5 of 5

Thread: widget doesnt show

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default widget doesnt show

    Hi,

    I am probably missing something very simple, but i cant figure out what it is.

    I have a QMainWIndow MyMainWindow, which shows up just fine. A toolbar holds an action.
    When i click on the toolbar item, the action is triggered correctly, and the slot it is connected to is executed. The slot is a member of MyMainWindow.
    The slot looks like this:


    Qt Code:
    1. bool MyMainWin::myslot()
    2. {
    3. MyWidget c(this);
    4. //c.setWindowModality(Qt::WindowModal);
    5. c.show();
    6. return 1;
    7. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, i just want it to show my custom widget "MyWidget", but it doesnt work.
    It just steps over the c.show() call, but doesnt show anything.

    I am using Qt 4.8.2 with VisualStudio 2008 and the QtAddin. Bot widgets/windows have been designed with the QtDesigner. The custom window shows just fine when i place above code right in main().

    What am i missing?

  2. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: widget doesnt show

    You are creating MyWidget on the stack.
    try creating it on the heap.
    Qt Code:
    1. MyWidget *c=new Mywidget(this);
    2. c->show();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: widget doesnt show

    MyWidget is a local variable, so it's going to be destroyed after exiting out of "myslot" scope.
    You have to create a widget in the heap (e.g. using new).
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: widget doesnt show

    yes, that was it. thanks.
    Should have seen that myself... :S


    Added after 14 minutes:


    Although, i then have a memoryleak, dont i?
    Who deletes the widget, once it is closed?
    This doesnt:

    void MyWidget::CancelClick()
    {
    this->close();
    }

    And there is another really wired problem:
    When i set the parent to mainwindow

    MyWidget *c=new Mywidget(this);

    the widget doesnt show properly. It doesnt become a window of its own, but is somehow fixed to mainwindow. Also, only the controls are shown, the widet doesnt have a background. Really strange.
    if i use

    MyWidget *c=new Mywidget(0);

    it works fine...
    Last edited by tuli; 5th September 2012 at 11:04.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: widget doesnt show

    If you give a widget a parent, then it wont be its own window - it will be 'inside' the parent.

    If you wish the widget to be destroyed on close, then you need to set the WA_deleteonclose widget attribute.

    http://doc.qt.io/qt-4.8/Qt#WidgetAttribute-enum
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 3
    Last Post: 2nd August 2011, 21:15
  2. How to not show widget
    By gQt in forum Qt Programming
    Replies: 9
    Last Post: 10th June 2010, 19:59
  3. Replies: 10
    Last Post: 29th May 2010, 18:42
  4. QWT doesnt show any windows
    By Cal in forum Qwt
    Replies: 5
    Last Post: 30th June 2009, 17:05
  5. showMaximized doesnt show the canvas
    By Kapil in forum Qt Programming
    Replies: 5
    Last Post: 23rd May 2006, 12:22

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.