Results 1 to 6 of 6

Thread: QPushButton if passed by reference to initialize, app crashing on connect statement

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QPushButton if passed by reference to initialize, app crashing on connect statement

    I have written a function which takes argument as QPushButton pointer, to initialize and apply stylesheet. And later wrote connect statements for the corresponding buttons. On running the application, it is crashing at connect statement. On debugging it's showing segmentation fault. If I comment the connect statements the application runs fine.

    If I can see the buttons laid out & also styles applied to them, then why is it giving the segmentation fault error which resembles for unallocated pointers ?
    Qt Code:
    1. MyWidget::MyWidget(QWidget * parent)
    2. : QWidget(parent)
    3. {
    4. vLyt = new QVBoxlayout;
    5. this->setLayout(vLyt);
    6. addButtons(btn1, "background: green");
    7. addButtons(btn2, "background: white");
    8. addButtons(btn3, "background: black");
    9. addButtons(btn4, "background: blue");
    10.  
    11. connect(btn1, SIGNAL(clicked(bool)), this, SLOT(mySlot1(bool))); // <<--- In debug mode, it's crashing here
    12. connect(btn2, SIGNAL(clicked(bool)), this, SLOT(mySlot2(bool)));
    13. connect(btn3, SIGNAL(clicked(bool)), this, SLOT(mySlot3(bool)));
    14. connect(btn4, SIGNAL(clicked(bool)), this, SLOT(mySlot4(bool)));
    15. }
    16.  
    17. void MyWidget::addButtons(QPushButton * pb, QString str)
    18. {
    19. pb = new QPushButton;
    20. pb->setCheckable(true);
    21. pb->setStyleSheet(str);
    22. vLyt->addWidget(pb);
    23. }
    To copy to clipboard, switch view to plain text mode 

    Why does the application crash at connect statement ?
    Last edited by rawfool; 6th March 2014 at 09:32.

  2. #2
    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: QPushButton if passed by reference to initialize, app crashing on connect stateme

    Quote Originally Posted by rawfool View Post
    Why does the application crash at connect statement ?
    Hint: check the value of btn1 before and after
    Qt Code:
    1. addButtons(btn1, "background: green");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPushButton if passed by reference to initialize, app crashing on connect stateme

    Oh got it.
    Now I changed it like this & it's working fine.
    Qt Code:
    1. MyWidget::MyWidget(QWidget * parent)
    2. : QWidget(parent)
    3. {
    4. ...
    5. ...
    6. btn1 = addButtons( "background: green");
    7. }
    8.  
    9. QToolButton * MyWidget::addButtons(QString str)
    10. {
    11. pb->setCheckable(true);
    12. pb->setStyleSheet(str);
    13. vLyt->addWidget(pb); // <<--- Adding the button to the layout here. Is it ok ??
    14.  
    15. return pb;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rawfool; 6th March 2014 at 10:51.

  4. #4
    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: QPushButton if passed by reference to initialize, app crashing on connect stateme

    That doesn't look like something that would compile.

    "pb" is of type QPushButton* but the method return value is of type QToolButton*, so either of the two needs to be changed.

    Adding the button to the layout in there should be OK

    Cheers,
    _

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

    rawfool (7th March 2014)

  6. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPushButton if passed by reference to initialize, app crashing on connect stateme

    Oh sorry for the typo. I meant QToolButton there.
    Thank you.

  7. #6
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QPushButton if passed by reference to initialize, app crashing on connect stateme

    To your code from the first post: You don't pass a QPushButton by reference. You pass "a pointer to QPushButton by value". That's why btn1/2/3/4 doesn't change after calling addButtons - just a copy of that pointer changes.
    The following piece of code might work.
    Qt Code:
    1. void MyWidget::addButtons(QPushButton *&pb, const QString &str)
    2. {
    3. pb = new QPushButton(this);
    4. pb->setCheckable(true);
    5. pb->setStyleSheet(str);
    6. vLyt->addWidget(pb);
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. reference to connect is ambiguous QQuickItem QMainWindow QCustomPlot
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 29th January 2014, 14:04
  2. Replies: 1
    Last Post: 22nd August 2013, 16:49
  3. Connect statement problem
    By Bender_Rodriguez in forum Newbie
    Replies: 4
    Last Post: 14th April 2013, 20:34
  4. Replies: 2
    Last Post: 9th May 2011, 10:38
  5. Slots & Signals w/ parameters passed by reference.
    By Wazman in forum Qt Programming
    Replies: 7
    Last Post: 20th December 2008, 00:13

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.