Results 1 to 8 of 8

Thread: Connect Slot in Subclass dont work !

  1. #1

    Question Connect Slot in Subclass dont work !

    Qt Code:
    1. class Lines : public QMainWindow {
    2. Q_OBJECT
    3. public:
    4. Lines(QWidget *parent = NULL);
    5. ~Lines();
    6. QWidget *widget_main;};
    7.  
    8. Lines::Lines(QWidget *parent) : QMainWindow ( parent ){
    9. ui.setupUi(this);
    10. widget_main = new QWidget(ui.centralwidget);
    11. Dummy d(this);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class Dummy : public QMainWindow {
    2. Q_OBJECT
    3. public:
    4. QPushButton *pushButton;
    5. explicit Dummy(Lines *parent = 0);
    6.  
    7. public slots: void slot_pushButton(); };
    8.  
    9. Dummy::Dummy(Lines *parent) : QMainWindow(parent){
    10. pushButton = new QPushButton(parent->widget_main);
    11. connect(pushButton, SIGNAL(clicked()), this->parent(), SLOT(slot_pushButton()));
    12. }
    13.  
    14. void Dummy::slot_pushButton(){
    15.  
    16. pushButton->hide();
    17.  
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    WHatever I do, I never jump into the slot_pushButton()

    However, If I declare and define slot_pushButton() in the class Lines it does work,
    but this is not what I want. I want to reach the slot_pushButton in the Subclass

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect Slot in Subclass dont work !

    connect(pushButton, SIGNAL(clicked()), this->parent(), SLOT(slot_pushButton()));
    this->parent() is the problem .. the third argument
    make just this ..
    "Behind every great fortune lies a crime" - Balzac

  3. #3

    Default Re: Connect Slot in Subclass dont work !

    I tried just "this" and it doesnt work.



    If I make this->parent() I can AT LEAST connect it to the slot of the class Lines.

    If I make only "this" nothing works at all.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connect Slot in Subclass dont work !

    The Dummy object is constructed and connects a clicked() signal to the slot_pushButton() of its parent() QObject. In the code we can see that would be an object of class Lines. That class does not have a slot called slot_pushButton() and you should be seeing a run time warning to that effect. Connections between QObjects are destroyed when the object at either end is destroyed but since this connection is never made...

    If you connect to clicked() signal to the slot of the Dummy class, i.e. this, you will at least establish a connection (as wagmare points out). However, your Dummy object is created, constructed and destroyed in the space of one source code line. Connections between QObjects are destroyed when the object at either end is destroyed, in this case the Dummy object is destroyed. The push button widget itself only persists because you are parenting it to another widget that persists past the demise of Dummy.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  5. The following user says thank you to ChrisW67 for this useful post:

    wagmare (2nd September 2013)

  6. #5

    Default Re: Connect Slot in Subclass dont work !

    I read your answer carefully, ChrisW67
    and I think I understood it.

    So it is simply not possible to do it the way I wanted, right??


    uhmmm...
    I tried something similar.

    Instead of calling my Dummy class from the Constructor of the Line class,
    I called my Dummy class from the main.

    Main
    Qt Code:
    1. int main (int argc, char*argv[] )
    2. {
    3. QApplication a(argc, argv);
    4. a.connect (&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    5. Lines w;
    6. Dummy d;
    7. w.show();
    8. return a.exec();
    To copy to clipboard, switch view to plain text mode 



    If I do like this, I can use the Slots in the Dummy class, but I cannot paint the pushbutton from my Dummy class on the widget from my Mainclass.


    Qt Code:
    1. pushButton = new QPushButton(parent->widget_main); //<<-- here the debugger crashes
    To copy to clipboard, switch view to plain text mode 


    Any Idea how to solve it?

    This is what I want to achieve if you shouldnt understand me

    4t2yahaa.jpg

    http://s14.directupload.net/images/130902/4t2yahaa.jpg

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connect Slot in Subclass dont work !

    Yes, make Dummy a QWidget and insert an instance of Dummy into the layout of the widget you wish to contain it. Just like every example in the Qt documentation.

  8. #7

    Default Re: Connect Slot in Subclass dont work !

    uhmm, could you be so kind and explain me how to insert the instance?

    It just crashes every time I reach the

    "pushButton = new QPushButton(parent->widget_main);"

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Slot in Subclass dont work !

    Quote Originally Posted by MrNoway View Post
    It just crashes every time I reach the

    "pushButton = new QPushButton(parent->widget_main);"
    it crashed because parent is a null pointer.

    If you want to access the pointer, pass a valid pointer.

    Even better follow ChrisW67's advice: make Dummy a QWidget subclass and properly put it into your central widget.

    Cheers,
    _

Similar Threads

  1. QWidget -> updateGeometry dont work.
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 22nd May 2013, 15:55
  2. Replies: 3
    Last Post: 9th October 2012, 02:12
  3. keypressevent dont work when MainWindow is minimized
    By spitty_cash in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2011, 16:13
  4. Dont work replot() after using zoom
    By ruzik in forum Qwt
    Replies: 2
    Last Post: 25th September 2011, 09:26
  5. Problem with QtBrowserPlugin - examples dont work.
    By robert_ugo in forum Installation and Deployment
    Replies: 0
    Last Post: 19th March 2009, 19:07

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.