Results 1 to 2 of 2

Thread: Problem with my own widget

  1. #1
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Problem with my own widget

    Hello,
    Im new to Qt, today i created very simple widget:
    Qt Code:
    1. class MyWidget : public QWidget{
    2. public:
    3. MyWidget(QWidget *parent=0);
    4. };
    5.  
    6. MyWidget::MyWidget(QWidget *parent)
    7. :QWidget(parent)
    8. {
    9. setFixedSize(200, 100);
    10. QPushButton *button=new QPushButton(tr("Przycisk"), this);
    11. button->setGeometry(20, 30, 100, 50);
    12. }
    To copy to clipboard, switch view to plain text mode 
    As you can see, this widget is only window and button placed in it.

    I added 2 lines (10 and 11) to main.cpp to display this widget:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. MyWidget widget;
    11. widget.show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    It works fine, but now I'd like to display MyWidget when user click on button placed on MainWindow.
    I created "buttonDown()" slot and connected it with pushButton from MainWindow:
    Qt Code:
    1. void MainWindow::buttonDown(){
    2. MyWidget widget();
    3. widget.show();
    4. }
    To copy to clipboard, switch view to plain text mode 
    But when I click on button nothing happens
    So displaying this widget from main function works fine, but displaying it from another function in exactly the same way doesn't.
    What should i do?

    PS. The whole code have been generated by Qt Creator.
    I have only added MyWidget class, and buttonDown slot.
    Sorry for bad english.


    @EDIT
    I solved problem, but I still need help.
    I replaced those lines:
    Qt Code:
    1. MyWidget widget;
    2. widget.show();
    To copy to clipboard, switch view to plain text mode 
    By these:
    Qt Code:
    1. MyWidget widget=new MyWidget;
    2. widget->show();
    To copy to clipboard, switch view to plain text mode 
    And it works now, but what is bad in first method?
    I thought it doesn't make any difference.
    Last edited by Macok; 24th January 2009 at 23:21.

  2. #2
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with my own widget

    Quote Originally Posted by Macok View Post
    Qt Code:
    1. MyWidget widget;
    2. widget.show();
    To copy to clipboard, switch view to plain text mode 
    Here you create the widget on the stack. When the method exits, widget gets out of scope and is destroyed immediately, hence you don't see anything.

    Quote Originally Posted by Macok View Post
    Qt Code:
    1. MyWidget widget=new MyWidget;
    2. widget->show();
    To copy to clipboard, switch view to plain text mode 
    This creates widget on the heap, which means it won't be destroyed when the method exits.

    I suggest you read up on C++ and scopes.

  3. The following user says thank you to rexi for this useful post:

    Macok (24th January 2009)

Similar Threads

  1. How to Open & Close a Widget ?!!
    By Fatla in forum Qt Programming
    Replies: 6
    Last Post: 13th June 2008, 20:39
  2. QT 4.2 widget setmask problem
    By PiXeL16 in forum Qt Programming
    Replies: 16
    Last Post: 2nd March 2007, 07:09
  3. Rotation problem trying to draw a compass widget
    By yellowmat in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2007, 19:03
  4. Problem applying setWindowOpacity to a custom Widget
    By yellowmat in forum Qt Programming
    Replies: 8
    Last Post: 1st November 2006, 10:05
  5. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55

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.