Results 1 to 10 of 10

Thread: One popup menu added to few tool buttons

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default One popup menu added to few tool buttons

    Hi,

    In my application i have some tool buttons which have the same popup menu which include actions - open, copy, and remove.

    Qt Code:
    1. QMenu menu;
    2.  
    3. QAction * open = new QAction( "Open", &menu );
    4. open->setShortcuts( QKeySequence::Open );
    5.  
    6. //... other actions
    7.  
    8. menu.addAction( open );
    9. menu.addAction( copy );
    10. menu.addAction( remove );
    11.  
    12. ui->left->setPopupMode( QToolButton::DelayedPopup );
    13. ui->left->setMenu( &menu );
    14.  
    15. ui->mid->setPopupMode( QToolButton::DelayedPopup );
    16. ui->mid->setMenu( &menu );
    17.  
    18. ui->right->setPopupMode( QToolButton::DelayedPopup );
    19. ui->right->setMenu( &menu );
    20.  
    21. // connects
    22.  
    23. connect( open, SIGNAL(triggered()), this, SLOT(open()) );
    To copy to clipboard, switch view to plain text mode 

    Now if user select some action in menu on some button I don't know how I can detect which of buttons relates this action.

    How I can do this?
    Thanks,

  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: One popup menu added to few tool buttons

    If they are the same action, then the trigger event does not matter.

    If it does then they are not the same action.

    But you could try checking the current focus widget.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: One popup menu added to few tool buttons

    Every button represents file in the filesystem and contains full path.
    Please look for these images.
    example1.pngexample2.png

    Functions open, copy and remove take QString as parametr so I need know which path send.
    I hope you understand me.

  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: One popup menu added to few tool buttons

    When you create the buttons, create a menu and actions for each.
    You can e.g. use QSIgnalMapper to map the otherwise parameterless triggered() signals to a slot that takes the filename or set the filename as data on each action and retrieve the sending action using QObject::sender() in the slot, or connect to the menu's signals

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: One popup menu added to few tool buttons

    Unfortunately QObject::sender() return QAction which I clicked not button which relates.
    I can write separate menu with separate actions to every button but then I have duplicate
    practically the same code for every button...

    this is my example code

    .h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QMenu>
    6. #include <QDebug>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. void createMenu();
    21.  
    22. public slots:
    23. void open();
    24. void copy();
    25. void remove();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29. QMenu menu;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    and .cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. createMenu();
    11.  
    12. ui->button1->setMenu( &menu );
    13. ui->button2->setMenu( &menu );
    14. ui->button3->setMenu( &menu );
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22.  
    23. void MainWindow::createMenu()
    24. {
    25. menu.setObjectName( "menu" );
    26.  
    27. QAction * open = new QAction( "Open", &menu );
    28. open->setObjectName( "open" );
    29. open->setShortcuts( QKeySequence::Open );
    30.  
    31. connect( open, SIGNAL(triggered()), this, SLOT(open()) );
    32.  
    33. QAction * copy = new QAction( "Copy", &menu );
    34. copy->setObjectName( "copy" );
    35. copy->setShortcuts( QKeySequence::Copy );
    36.  
    37. connect( copy, SIGNAL(triggered()), this, SLOT(copy()) );
    38.  
    39. QAction * remove = new QAction( "Remove", &menu );
    40. remove->setObjectName( "remove" );
    41. remove->setShortcuts(QKeySequence::Delete );
    42.  
    43. connect( remove, SIGNAL(triggered()), this, SLOT(remove()) );
    44.  
    45. menu.addAction( open );
    46. menu.addAction( copy );
    47. menu.addAction( remove );
    48.  
    49. }
    50.  
    51. void MainWindow::open()
    52. {
    53. qDebug()<< QObject::sender()->objectName();
    54. }
    55.  
    56. void MainWindow::copy()
    57. {
    58. qDebug()<< QObject::sender()->objectName();
    59. }
    60.  
    61. void MainWindow::remove()
    62. {
    63.  
    64. qDebug()<< QObject::sender()->objectName();
    65. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by atomic; 12th August 2014 at 13:08.

  6. #6
    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: One popup menu added to few tool buttons

    Quote Originally Posted by atomic View Post
    Unfortunately QObject::sender() return QAction which I clicked not button which relates.
    Obviously, it is the sender of the signal that triggered the slot. It is also what I wrote.

    For the respective option the reference information would be attached to the action as user data, see QAction::setData).

    Quote Originally Posted by atomic View Post
    I can write separate menu with separate actions to every button but then I have duplicate
    practically the same code for every button...
    Exactly, ideally as a component and just instantiated multiple times.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: One popup menu added to few tool buttons

    Or you don't understand me and my problem or... I don't know.
    You say that the best solution is write for each button sparate menu and actions?
    And have in class 3 menu and 3 open, copy and remove functions? please no...
    I can't agree with you.

    Look what i have now and comment this please

    I create enum class
    Qt Code:
    1. enum class Side {
    2. left, right
    3. } activeSide;
    To copy to clipboard, switch view to plain text mode 

    added all buttons to QButtonGroup with id
    Qt Code:
    1. buttonGroup.addButton( ui->button1, 1 );
    2. buttonGroup.addButton( ui->button2, 2 );
    To copy to clipboard, switch view to plain text mode 

    next connected buttonGroup signal buttonPressed to slot in my class where i detect active button
    Qt Code:
    1. connect( &buttonGroup, SIGNAL(buttonPressed(int)) , this,
    2. SLOT(registerId(int)) );
    3.  
    4. void MainWindow::registerId(int currentButton )
    5. {
    6. switch( currentButton ) {
    7. case 1:
    8. activeSide = Side::left;
    9. break;
    10. case 2:
    11. activeSide = Side::right;
    12. break;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    and now i can create only 2 function( independent of number of buttons ).
    - one without parameters
    - one with parameters as QString

    Qt Code:
    1. void MainWindow::open()
    2. {
    3. switch( activeSide ) {
    4. case Side::left:
    5. open( ui->button1.text() );
    6. break;
    7. case Side::right:
    8. open( ui->button2.text() );
    9. break;
    10. }
    11. }
    12.  
    13. void MainWindow::open(const QString &path)
    14. {
    15. QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );
    16. }
    To copy to clipboard, switch view to plain text mode 

    What do you think about this code?
    Regards,

  8. #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: One popup menu added to few tool buttons

    You could even use the enum value as the id.

    Cheers,
    _

  9. #9
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: One popup menu added to few tool buttons

    In QButtonGroup instead int?
    But then this must be simple enum not enum class to provide conversion between string and int.
    Is this what you mean?

  10. #10
    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: One popup menu added to few tool buttons

    Yes. Would remove the necessity of a switch, the int would be the value you are looking for.

    Cheers,
    _

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

    atomic (13th August 2014)

Similar Threads

  1. Replies: 5
    Last Post: 29th November 2012, 15:26
  2. Replies: 2
    Last Post: 28th September 2012, 17:15
  3. Replies: 3
    Last Post: 17th May 2009, 20:17
  4. Grouping of Tool buttons in Tool bar
    By febil in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2009, 11:51
  5. Tool Buttons...
    By csvivek in forum Qt Tools
    Replies: 1
    Last Post: 9th May 2008, 07:44

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.