Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, August 2006
Conditional SubMenu - a submenu that can be Triggered


Message 1 in thread

Hi all !

I would like to have a menu item that is both an Action and a Submenu.
Ie : a menu item that can both "be triggered" and "used to open its
submenu".

When cliking on the arrow, or stop moving over the submenu item, the submenu
attached to the menu item would be displayed - as with a common submenu.

When clicking on this menu item, this menu item itself would be "triggered"
- as a usual action is. 
A sort of "default triggering" for the submenu.


These feature are available in various software, and are very efficient to
me.

How can I do that with Qt 4 ?
I did not find a solution...


As a remark, the same question was posted on 2000. 
See for reference and details :
http://lists.trolltech.com/qt-interest/2000-03/thread00528-0.html


All the best,
Nicolas

PS : I post quite a lot these days. I wish these posts are relevant (I try
to make them so !)

--
 [ signature omitted ] 

Message 2 in thread

On 23.08.06 19:23:00, Nicolas Castagne wrote:
> I would like to have a menu item that is both an Action and a Submenu.
> Ie : a menu item that can both "be triggered" and "used to open its
> submenu".

QToolButton can do this, though I'm not sure wether it can be easily embedded
in a QMenu.

It might also be possible to use QAction for this directly as it has a
setMenu function. Just try it out.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

Thx Andreas.

I have tried all I could. Still unsuccessful.

Included, an unsuccesful simple samplecode.

I still cannot find how to get a QAction that both :
- can be "triggerable" by the user
- can allow displyaing a submenu
depending on the user's actions.

In my example, this action can be triggered programmatically.
But I am still unable to let the user trigger it... (at least on my Linux).

In case anyone can propose a solution...

Best-
Nicolas

Andreas Pakulat wrote:

> On 23.08.06 19:23:00, Nicolas Castagne wrote:
>> I would like to have a menu item that is both an Action and a Submenu.
>> Ie : a menu item that can both "be triggered" and "used to open its
>> submenu".
> 
> QToolButton can do this, though I'm not sure wether it can be easily
> embedded in a QMenu.
> 
> It might also be possible to use QAction for this directly as it has a
> setMenu function. Just try it out.
> 
> Andreas
> 

Attachment:

Attachment: triggerSubMenu.tar
Description: Unix tar archive


Message 4 in thread

On 24.08.06 10:55:49, Nicolas Castagne wrote:
> Thx Andreas.
> 
> I have tried all I could. Still unsuccessful.
> 
> Included, an unsuccesful simple samplecode.
> 
> I still cannot find how to get a QAction that both :
> - can be "triggerable" by the user
> - can allow displyaing a submenu
> depending on the user's actions.
> 
> In my example, this action can be triggered programmatically.
> But I am still unable to let the user trigger it... (at least on my Linux).
> 
> In case anyone can propose a solution...

Wait for Qt4.2, it'll contain a new Action QWidgetAction which allows to
use a QWidget. I'll attach 2 files with the mainwindow class. It's not
really nice but all you'll get.

For Qt4.1 theres probably no way to do it.

Andreas

-- 
 [ signature omitted ] 
/****************************************************************************
**
** Copyright (C) 2004-2006 Trolltech AS. All rights reserved.
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@xxxxxxxxxxxxxx
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include <QtGui>
#include <QWidgetAction>

#include "mainwindow.h"

MainWindow::MainWindow()
{
    QWidget *w = new QWidget;
    setCentralWidget(w);

    createMenu();

    setWindowTitle(tr("toward a submenu that can be triggered !"));

    statusBar()->showMessage(tr("toward a submenu that can be triggered !"));

    setMinimumSize(160, 160);
    resize(480, 320);


    // you can trigger the mainAction programmatically...
    // but how does the user triggers it ??
	qDebug() ;
	qDebug() << "Indeed, it is possible trigger the mainAction programmatically" ;
	qDebug() << "but how does the user triggers it" ;
	qDebug() ;
	qDebug() << "As example : triggering mainAction programmatically now !" ;
	qDebug() ;
    mainAction->trigger();
}

MainWindow::~MainWindow() {
	delete subMenu;
}

void MainWindow::onMainAction()
{
	qDebug() << "mainAction triggered" ;
   	statusBar()->showMessage(tr("mainAction triggered !"));
}


void MainWindow::onSubAction()
{
	qDebug() << "subAction triggered" ;
    	statusBar()->showMessage(tr("subAction triggered !"));
}




void MainWindow::createMenu()
{
    // the actions
    mainAction = new QWidgetAction(this);
 //   connect(mainAction, SIGNAL(triggered()), this, SLOT(onMainAction()));



    subAction = new QAction(tr("subAction"), this);
    subAction->setStatusTip(tr("subAction"));
    connect(subAction, SIGNAL(triggered()), this, SLOT(onSubAction()));

    subMenu = new QMenu() ;
    subMenu->addAction(subAction);
    QToolButton* tb = new QToolButton();
    tb->setText( "MainAction" );
    tb->setMenu( subMenu );
    tb->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
//    tb->setPopupMode( QToolButton::MenuButtonPopup );
    tb->setArrowType( Qt::RightArrow );
    connect( tb, SIGNAL( clicked() ), this, SLOT( onMainAction() ) );

    mainAction->setDefaultWidget( tb );

    // the menus

    mainMenu = menuBar()->addMenu(tr("&MainMenu"));
    mainMenu->addAction(mainAction);


    //subMenu = mainMenu->addMenu(tr("&subMenu"));

    mainAction->setMenu(subMenu);

}
/****************************************************************************
/****************************************************************************
**
** Copyright (C) 2004-2006 Trolltech AS. All rights reserved.
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@xxxxxxxxxxxxxx
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QWidgetAction;
class QAction;
class QLabel;
class QMenu;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();
    ~MainWindow();

protected:

private slots:
    void onMainAction();
    void onSubAction();


private:
    void createMenu();

    QMenu *mainMenu;
    QWidgetAction *mainAction;

    QMenu *subMenu;
    QAction *subAction;

};

#endif

Message 5 in thread

Hi Nicolas & Andreas,

Andreas Pakulat schrieb:
> On 24.08.06 10:55:49, Nicolas Castagne wrote:
>   
>> Thx Andreas.
>>
>> I have tried all I could. Still unsuccessful.
>>
>> Included, an unsuccesful simple samplecode.
>>
>> I still cannot find how to get a QAction that both :
>> - can be "triggerable" by the user
>> - can allow displyaing a submenu
>> depending on the user's actions.
>>
>> In my example, this action can be triggered programmatically.
>> But I am still unable to let the user trigger it... (at least on my Linux).
>>
>> In case anyone can propose a solution...
>>     
>
> Wait for Qt4.2, it'll contain a new Action QWidgetAction which allows to
> use a QWidget. I'll attach 2 files with the mainwindow class. It's not
> really nice but all you'll get.
>
> For Qt4.1 theres probably no way to do it.
>
>   
I have written a class which adds a submenu (a button menu in this case)
to a tool button. I also wrote a
custom toolbar to be able to relatively easy add something to a toolbar.
The code is part of the Canorus project
(http://canorus.berlios.de) and can be found in the svn (release is due
to this evening).
The code is far from perfect, the main problem is that I could not
change the menu position in an easy manner
(I guess I would have to reimplement the paintEvent, therefore I've
downloaded the Qt4.1.4 source but haven't
had time to look closer if it is a good idea to do so).

For those interested: The file can be found in
src/widgets/toolbar.h/cpp, it is used in src/ui/mainwin.cpp.
(WebSVN access: http://svn.berlios.de/wsvn/canorus/trunk/?rev=0&sc=0).
The class for the custom toolbar
is called CAToolBar, the class for the custom button menu (based on
QMenu) is called CAButtonMenu.
You can use eclipse for browsing the code, see
http://canorus.berlios.de/wiki/index.php/Eclipse.

I have yet to check the example code Andreas posted here.

Best regards,

-- 
 [ signature omitted ]