Archived:Creating Context Menu from QPushButton
hamishwillee
(Talk | contribs) m (Hamishwillee - Addition to article of: Category:MeeGo Category:Symbian. (Add platform categories)) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot update - Fix ArticleMetaData and other issues) |
||
| Line 1: | Line 1: | ||
| − | [[Category: | + | [[Category:Qt C++ UI]][[Category:UI]][[Category:How To]][[Category:Code Examples]] |
{{Abstract|This article shows how to create a popup menu using a "styled" push button in a {{Icode|QWidget}} based application.}} The menu is launched using a {{Icode|QPushButton}}. | {{Abstract|This article shows how to create a popup menu using a "styled" push button in a {{Icode|QWidget}} based application.}} The menu is launched using a {{Icode|QPushButton}}. | ||
{{Archived|timestamp=20120213032138|user=[[User:Hamishwillee|<br />----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on {{Qapiname|QWidget}}) is deprecated.}} | {{Archived|timestamp=20120213032138|user=[[User:Hamishwillee|<br />----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on {{Qapiname|QWidget}}) is deprecated.}} | ||
| − | {{ArticleMetaData <!-- v1. | + | {{ArticleMetaData <!-- v1.2 --> |
|sourcecode= [[Media:Button.zip]] | |sourcecode= [[Media:Button.zip]] | ||
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| Line 11: | Line 11: | ||
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | |signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | ||
| − | |capabilities= <!-- Capabilities required (e.g. Location, NetworkServices. | + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> |
|keywords= QPushButton | |keywords= QPushButton | ||
| − | |||
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
|translated-by= <!-- [[User:XXXX]] --> | |translated-by= <!-- [[User:XXXX]] --> | ||
| Line 85: | Line 84: | ||
</code> | </code> | ||
| − | In the constructor we create a {{Icode|QWidget}} (widget) of size '''400x500''' along with a {{Icode|QHBoxLayout}} (Layout). We | + | In the constructor we create a {{Icode|QWidget}} (widget) of size '''400x500''' along with a {{Icode|QHBoxLayout}} (Layout). We create the {{Icode|QMenu}} (menu) and add items in the menu. Button have been created using {{Icode|QPushButton}} and add the button to the widget using {{Icode|addWidget()}} and then set the widget layout through {{Icode|setLayout()}} method . {{Icode|setStyleSheet()}} property holds the widget's style sheet. |
| Line 91: | Line 90: | ||
==Screenshot== | ==Screenshot== | ||
| − | [[ | + | [[File:Mybutton001001.jpg]] |
==Source Code== | ==Source Code== | ||
| Line 98: | Line 97: | ||
==Related Links== | ==Related Links== | ||
| − | * [[ | + | * [[How to use QPushButton (Qt)|How to use QPushButton]][[Category:MeeGo]] [[Category:Symbian]] |
Revision as of 08:47, 5 March 2012
This article shows how to create a popup menu using a "styled" push button in a QWidget based application. The menu is launched using a QPushButton.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Class Definition
#include <QMainWindow>
#include "QHBoxLayout"
#include "QPushButton"
namespace Ui {
class Button;
}
class Button : public QMainWindow
{
Q_OBJECT
public:
explicit Button(QWidget *parent = 0);
~Button();
private:
QWidget* win;
QHBoxLayout *lay;
QMenu *menu;
QPushButton* but1;
};
#endif // BUTTON_H
We implement the Push Button in a class called Button, which we derive from QMainWindow. Here we are particularly interested the constructor, so lets discuss below about the constructor in the Class Implementation.
Class Implementation
#include "button.h"
Button::Button(QWidget *parent) :
QMainWindow(parent)
{
win=new QWidget(this);
win->resize(400,500);
lay=new QHBoxLayout(this);
menu=new QMenu(this);
menu->addMenu("Nokia");
menu->addMenu("N96");
menu->addMenu("N97");
but1=new QPushButton("Menu",this);
but1->setMenu(menu);
lay->addWidget(but1);
win->setLayout(lay);
win->setStyleSheet("* { background-color:rgb(125,100,50);color:rgb(200,150,100); padding: 7px}}");
}
Button::~Button()
{
}
In the constructor we create a QWidget (widget) of size 400x500 along with a QHBoxLayout (Layout). We create the QMenu (menu) and add items in the menu. Button have been created using QPushButton and add the button to the widget using addWidget() and then set the widget layout through setLayout() method . setStyleSheet() property holds the widget's style sheet.
Screenshot
Source Code
The full source code presented in this article is available here File:Button.zip


