Results 1 to 7 of 7

Thread: attempting to connect button to aboutQt()

  1. #1
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default attempting to connect button to aboutQt()

    Hi All,

    I am attempting to set up a signal/slot arrangement for a button.

    I read a recommendation somewhere that a way to debug the signal part of a connection statement was to do something like:

    Qt Code:
    1. connect(myObj, SIGNAL(mySignal()), qApp, SLOT(aboutQt()));
    To copy to clipboard, switch view to plain text mode 

    I thought I knew a good idea when I saw one. Anyway, I encounter a problem with this trifling task.


    Qt Code:
    1. class ButtonDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ButtonDelegate(QObject *parent=0);
    6. ~ButtonDelegate();
    7.  
    8. // code redacted for bevity
    9.  
    10. private:
    11. QPushButton *button;
    12.  
    13. };
    14.  
    15. ButtonDelegate::ButtonDelegate(QObject *parent) : QStyledItemDelegate(parent)
    16. {
    17. std::cout << "in ButtonDelegate() ctor: \n" << endl;
    18. connect(button, SIGNAL(clicked()), qApp, SLOT(aboutQt()));
    19. std::cout << "after ButtonDelegate() ctor is complete \n" << endl;
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    This code crashes at the connect statement. "in ButtonDelegate()..." is printed to the console and "after ButtonDelegate().." is not printed.

    Does anyone have an idea what I am getting wrong?

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attempting to connect button to aboutQt()

    ajax, what are we going to do with you? Did you initialize button?

  3. #3
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: attempting to connect button to aboutQt()

    No the button is not initialized at this time. I thought that just the name of the pointer was sufficient for the connect() function. I gather from your question that I didn't have that right.

    this button is instantiated during the lifetime of the application if and only if the user hovers his mouse over a row in a treeview. Here is the code:

    Qt Code:
    1. QWidget * ButtonDelegate::createEditor( QWidget * parent,
    2. const QStyleOptionViewItem & option,
    3. const QModelIndex & index ) const
    4. {
    5. // if the current column is the 'share' button's column
    6. if ( index.column() == buttonColumn )
    7. { // create the button that will be located in this view item
    8. return new QPushButton("Share", parent);
    9. }
    10. else
    11. {
    12. return QStyledItemDelegate::createEditor(parent, option, index );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Now, as I understand it, createEditor() is invoked when openPersistentEditor() is invoked. Another detail, the spec calls for a 'hidden' button to be located in every cell in column zero of a spreadsheet-like view. I handle all of this in a mouseMoveEvent():

    Qt Code:
    1. void MyTreeView::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QAbstractItemModel *m(model());
    4.  
    5. // Only do something when a model is set.
    6. if (m)
    7. {
    8. QModelIndex index = this->indexAt(event->pos());
    9. if (index.isValid())
    10. {
    11.  
    12. // if the mouse has moved to another row
    13. if (index.row() != m_currentRow)
    14. {
    15. m_currentRow = index.row();
    16.  
    17. // clear buttons from all rows
    18. for ( int i = 0; i < m->rowCount(); ++i )
    19. {
    20. this->closePersistentEditor( m->index(i, 0, QModelIndex()) );
    21. }
    22.  
    23. // create button on mouse's current row
    24. this->openPersistentEditor(m->index(m_currentRow, 0, QModelIndex() ));
    25.  
    26.  
    27. }
    28. }
    29. else // model is invalid
    30. {
    31. m_currentRow = -1;
    32. }
    33. }
    34.  
    35. QTreeView::mouseMoveEvent(event);
    36. }
    To copy to clipboard, switch view to plain text mode 

    One other detail: The buttonDelegate class has a button pointer:

    Qt Code:
    1. class ButtonDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit ButtonDelegate(QObject *parent=0);
    7. ~ButtonDelegate();
    8.  
    9. // code redacted for bevity
    10. private:
    11.  
    12. QPushButton *button;
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 
    I'm faintly embarrassed to admit that I didn't notice the button pointer has evidently fallen out of use. This code had been through a number of major changes before wysota guided me to a solution that seemed to work. But that solution is rather different than my initial approach. It seems that the framework holds the pointer to the button for the duration of the editor's life.

    But, on the other hand, if I comment-out the references to the 'button pointer' the app crashes, and I don't know why.

    OK, Timoteo, based on your comment it appears that I need to create the connection that uses the button click as a signal in mouseMoveEvent(), just after the return from openPersistentEditor(). If so, does this mean that disconnect() should be called after closePersistentEditor() is called? I guess it would.

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

    Default Re: attempting to connect button to aboutQt()

    Quote Originally Posted by ajax View Post
    I thought that just the name of the pointer was sufficient for the connect() function.
    In C++ there is no such thing as "name of the pointer".

    This code had been through a number of major changes before wysota guided me to a solution that seemed to work.
    Please don't mention my name in posts that involve this "solution" you are using. This is a really bad approach and I didn't guide you to it. And even if I unintentionally did then I won't admit it.
    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
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attempting to connect button to aboutQt()

    I think you've already lost a handle on your ownership semantics (you do have them, right?). Take a few moments to go and do something else, maybe a spot of tea, and consider who should be owning what. Right now, it appears that you don't have a strategy in place.

    I also sense a fundamental lack of understanding of what a pointer truly is. Perhaps that is what you should think on first.

  6. #6
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: attempting to connect button to aboutQt()

    OK, guys, I do know what a pointer is. I have been programming in C for a lot of years and in C++, on and off, for several years. Since you want it in jargon, what I should have said something along the lines of "passed the pointer to the button in first argument to the call to the connect() function." And, of course, since the variables, including pointers, are uniquely identified by their names (pace, all quibbling about namespaces, scope, names in outer scopes being hidden by identical names in inter-scopes, etc., etc.) I just naively assumed that a phrase like "name of the pointer" could be used without bringing the language lawyers down on me. My bad.

    Regarding lack of strategy: you have a point. My problem is that this is my first Qt application and I am still working out how things are done in the Qt world. Since I am just beginning this process of discovery I am relatively clueless, but I believe that experience will cure that. Already a few of my initial approaches have exploded on contact with the way Qt actually does things. Of course I would rather have guessed correctly in the first place, but the necessary precondition of guessing correctly is to have the subject matter expertise that newbies have not yet obtained.

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

    Default Re: attempting to connect button to aboutQt()

    Quote Originally Posted by ajax View Post
    OK, guys, I do know what a pointer is. I have been programming in C for a lot of years and in C++, on and off, for several years. Since you want it in jargon, what I should have said something along the lines of "passed the pointer to the button in first argument to the call to the connect() function." And, of course, since the variables, including pointers, are uniquely identified by their names (pace, all quibbling about namespaces, scope, names in outer scopes being hidden by identical names in inter-scopes, etc., etc.) I just naively assumed that a phrase like "name of the pointer" could be used without bringing the language lawyers down on me. My bad.
    To be honest you didn't convince me. From what I understood you thought that if you pass a pointer variable named "x" to the connect statement and then you would assign some object to "x" then when Qt dereferences the "x" pointer it will be pointing to the object that you "in the meantime" assigned to "x" which leads straight to a conclusion that your understanding of pointers and function calls is not as perfect as you may have thought.

    Regarding lack of strategy: you have a point. My problem is that this is my first Qt application and I am still working out how things are done in the Qt world. Since I am just beginning this process of discovery I am relatively clueless, but I believe that experience will cure that. Already a few of my initial approaches have exploded on contact with the way Qt actually does things. Of course I would rather have guessed correctly in the first place, but the necessary precondition of guessing correctly is to have the subject matter expertise that newbies have not yet obtained.
    Let me make your decisions easier - if you create an editor for a model index, the view takes responsibility of it and there is no way you can change that. The object may get deleted behind your back at an arbitrary point in time.
    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.


Similar Threads

  1. Replies: 7
    Last Post: 11th May 2017, 00:09
  2. [PyQt4] Want to connect a window's close button
    By Dodle in forum Qt Programming
    Replies: 7
    Last Post: 21st April 2010, 11:13
  3. Connect button and pass QListView current index
    By been_1990 in forum Qt Programming
    Replies: 3
    Last Post: 30th November 2009, 16:20
  4. Replies: 2
    Last Post: 8th February 2008, 14:31
  5. Connect Button to a custom slot
    By Majestade in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 17:17

Tags for this Thread

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.