Results 1 to 7 of 7

Thread: What's wrong with my actions???

  1. #1
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default What's wrong with my actions???

    In a custom tree model I have enabled context menu handling at node level and the code looks like that :
    Qt Code:
    1. pMenu = new QMenu("Actions");
    2. setupMenu(pMenu);
    3.  
    4. QAction *a = pMenu->exec(p);
    5.  
    6. qDebug("action triggered : %s", a ? qPrintable(a->text()) : "");
    7. //pMenu->deleteLater();
    8.  
    9. if ( a )
    10. a->trigger();
    To copy to clipboard, switch view to plain text mode 

    setupMenu() is a virtual function that is in charge of setting up the menu content :
    Qt Code:
    1.  
    2. a = new QAction(QIcon(":/"), tr("&Remove"), m);
    3. connect(a , SIGNAL( triggered() ),
    4. this, SLOT ( deleteLater() ) );
    5.  
    6. m->addAction(a);
    7.  
    8. m->addSeparator();
    9.  
    10. a = new QAction(QIcon(":/folder.png"), tr("A&dd folder"), m);
    11. connect(a , SIGNAL( triggered() ),
    12. this, SLOT ( addFolder() ) );
    13.  
    14. m->addAction(a);
    15.  
    16. a = new QAction(QIcon(":/add.png"), tr("Add &file"), m);
    17. connect(a , SIGNAL( triggered() ),
    18. this, SLOT ( addFile() ) );
    19.  
    20. m->addAction(a);
    21.  
    22. a = new QAction(QIcon(":/file.png"), tr("&New file"), m);
    23. connect(a , SIGNAL( triggered() ),
    24. this, SLOT ( newFile() ) );
    25.  
    26. m->addAction(a);
    27.  
    28. m->addSeparator();
    29.  
    30. a = new QAction(QIcon(":/clear.png"), tr("&Clear"), m);
    31. connect(a , SIGNAL( triggered() ),
    32. this, SLOT ( clear() ) );
    33.  
    34. m->addAction(a);
    To copy to clipboard, switch view to plain text mode 

    When I play with my model on a QTreeView the context menu gets displayed correctly. Then if I trigger an action by clicking on a menu item I get a correct message on the console output ( i.e : "action triggered : x") but the triggered signal is never emitted (or never forwarded to the slots I have connected to it even when I call trigger() programmatically ...

    I'm using Qt 4.2.2 under Linux. Any hints???
    Current Qt projects : QCodeEdit, RotiDeCode

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with my actions???

    What is "this" in setupMenu()? What method you invoke "setupMenu(pMenu);" from?

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with my actions???

    I have a node (the typename doesn't matter here) inheriting from QObject. It has a void contextMenuEvent(const QPoint& p) called from the view (first block of code) and the setupMenu() is also a member of this class. The puzzling thing is that I tried substituting signal/slots with QMetaObject::invokeMethod but it did not give any result either...

    EDIT :
    Yeah!!! I've solved it! My fix is (very) dirty but inheritance-aware as I needed and AFAIK the Trolls have no reason changing the syntax of the meta-object system before a while...
    I suppose the problem comes from the fact that my nodes are created in a separate thread (to avoid GUI freeze) which gets destroyed afterward (thus no event loop is running). So I added a "QHash<QAction*, const char*> slot_table;" member which is filled in the setupMenu() method with appropriate action-method name pairs. Then, after menu execution I use the following code :
    Qt Code:
    1. QAction *a = m.exec(p);
    2.  
    3. if ( a && slot_table.contains(a) )
    4. {
    5. int idx = metaObject()->indexOfSlot(slot_table[a]);
    6. qt_metacall(QMetaObject::InvokeMetaMethod, idx, 0);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by fullmetalcoder; 4th March 2007 at 10:30.
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with my actions???

    Quote Originally Posted by fullmetalcoder View Post
    I suppose the problem comes from the fact that my nodes are created in a separate thread (to avoid GUI freeze) which gets destroyed afterward (thus no event loop is running).
    If the is no event loop running in that thread, queued connections won't work.

    Quote Originally Posted by fullmetalcoder View Post
    So I added a "QHash<QAction*, const char*> slot_table;" member which is filled in the setupMenu() method with appropriate action-method name pairs.
    Isn't that an equivalent of adding Qt::DirectConnection to connect statements?

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with my actions???

    Quote Originally Posted by jacek View Post
    If the is no event loop running in that thread, queued connections won't work.

    Isn't that an equivalent of adding Qt:irectConnection to connect statements?
    Well, I expected my slots to be called directly but I think I now understand why they were not even if it looks strange... My nodes have been created in a given (now defunct) thread and some of their methods are called from the GUI thread. In this case created objects will be attached to the GUI thread (or am I missing something??) and it will cause Queued Connections by default which won't work properly...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with my actions???

    Quote Originally Posted by fullmetalcoder View Post
    My nodes have been created in a given (now defunct) thread and some of their methods are called from the GUI thread.
    Maybe it will be enough if you simply move those objects to the GUI thread using QObject::moveToThread()?

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with my actions???

    Quote Originally Posted by jacek View Post
    Maybe it will be enough if you simply move those objects to the GUI thread using QObject::moveToThread()?
    I thought about that already but did not find any convininent way to do it in my code... Anyway I'm done now.
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Ideas about how to implement Undo and Redo actions?
    By pir in forum General Discussion
    Replies: 3
    Last Post: 21st July 2006, 09:27
  2. QListWidget...what's wrong
    By nupul in forum Newbie
    Replies: 16
    Last Post: 4th April 2006, 12:17
  3. Replies: 1
    Last Post: 18th March 2006, 16:37
  4. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:06
  5. Actions and what to do with them?
    By krsmichael in forum Qt Tools
    Replies: 2
    Last Post: 20th January 2006, 22:08

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.