PDA

View Full Version : QPushButton and QToolButoon in QToolBar



mythili
9th May 2013, 11:59
Hi,
How to add QPushButton and QToolButton in QToolBar in ui. Please help me

Santosh Reddy
9th May 2013, 12:22
Right-click on the main window and select Add toolbar from the context menu.

mythili
9th May 2013, 12:25
I am having Toolbar. I want to add QPushButton and QToolButton on ToolBar

Santosh Reddy
9th May 2013, 12:38
From UI designer it is not possible, from code you will need to use QWidgetAction

mythili
9th May 2013, 12:53
Thank u very much....
Can u give an example

Santosh Reddy
9th May 2013, 13:52
Just one example
9021


#include <QtGui>
#include <QApplication>

class PushButtonAction : public QWidgetAction
{
public:
explicit PushButtonAction(const QIcon & icon, const QString & text, QObject *parent = 0)
: QWidgetAction(parent)
{
setIcon(icon);
setObjectName(text);
}

protected:
virtual QWidget * createWidget(QWidget * parent)
{
return new QPushButton(icon(), objectName(), parent);
}

virtual void deleteWidget(QWidget * widget)
{
delete widget;
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QMainWindow mainWindow;

QToolBar * toolBar= new QToolBar("Main Window Tool Bar");
toolBar->insertAction(0, new PushButtonAction(QIcon(":/Refresh.gif"), "Refresh"));
toolBar->addAction(QIcon(":/First.gif"), "FirstAction");
toolBar->addAction(QIcon(":/Last.gif"), "LastAction");
toolBar->insertAction(0, new PushButtonAction(QIcon(":/Search.gif"), "Search"));

mainWindow.addToolBar(Qt::TopToolBarArea, toolBar);
mainWindow.showMaximized();

return app.exec();
}