×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Creating Context Menu from QPushButton

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
somnathbanik (Talk | contribs)
Line 5: Line 5:
 
|id=...
 
|id=...
 
|platform=S60 3rd Edition, S60 5th Edition
 
|platform=S60 3rd Edition, S60 5th Edition
|devices=Emulator
+
|devices=Emulator/ N8
 
|category=Qt
 
|category=Qt
 
|subcategory= UI
 
|subcategory= UI
Line 14: Line 14:
  
 
==Introduction==
 
==Introduction==
Associates the popup menu menu with this push button.
+
{{Abstract| This article will help us to know how can we create style popup menu using push button in [[Qt]] }}
  
This turns the button into a menu button, which in some styles will produce a small triangle to the right of the button's text.
+
For creating push button we will use {{Icode|QPushButton}}. This turns the push button into a menu button, which in some styles will produce a small triangle to the right of the button's text.
  
==Source Code==
+
 
== Source File==
+
== Class Definition ==
 
<code cpp>
 
<code cpp>
 +
#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
 +
 +
</code>
 +
We implement the Push Button in a class called '''Button''', which we derive from {{Icode|QMainWindow}}. Here we are particularly interested the constructor, so lets discuss below about the constructor in the Class Implementation.
 +
 +
 +
 +
== Class Implementation ==
 +
<code cpp>
 
#include "button.h"
 
#include "button.h"
#include "ui_button.h"
+
Button::Button(QWidget *parent) :
 +
    QMainWindow(parent)
  
button::button(QWidget *parent)
 
    : QWidget(parent)
 
 
{
 
{
  win=new QWidget(this);
+
 
  win->resize(400,500);
+
      win=new QWidget(this);
  lay=new QHBoxLayout(this);
+
      win->resize(400,500);
  menu=new QMenu(this);
+
      lay=new QHBoxLayout(this);
  menu->addMenu("Nokia");
+
      menu=new QMenu(this);
  menu->addMenu("N96");
+
      menu->addMenu("Nokia");
  menu->addMenu("N97");
+
      menu->addMenu("N96");
  but1=new QPushButton("Menu",this);
+
      menu->addMenu("N97");
  but1->setMenu(menu);
+
      but1=new QPushButton("Menu",this);
  lay->addWidget(but1);
+
      but1->setMenu(menu);
  win->setLayout(lay);
+
      lay->addWidget(but1);
  win->setStyleSheet("* { background-color:rgb(125,100,50);color:rgb(200,150,100); padding: 7px}}");
+
      win->setLayout(lay);
 +
      win->setStyleSheet("* { background-color:rgb(125,100,50);color:rgb(200,150,100); padding: 7px}}");
 
}
 
}
  
button::~button()
+
Button::~Button()
 
{
 
{
// No need to delete any object that has a parent which is properly deleted.
+
 
 
}
 
}
 
</code>
 
</code>
 +
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.
 +
 +
  
 
==Screenshot==
 
==Screenshot==
  
 
[[Image:Mybutton001001.jpg]]
 
[[Image:Mybutton001001.jpg]]
 +
 +
==Source Code==
 +
The full source code presented in this article is available here [[File:Button.zip]]
  
  

Revision as of 17:57, 10 May 2011

{{{width}}}
Article Metadata
Tested with
Devices(s): Emulator/ N8
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition
Symbian
Article
Keywords: QPushButton
Created: (27 Jun 2009)
Last edited: somnathbanik (10 May 2011)


Contents

Introduction

This article will help us to know how can we create style popup menu using push button in Qt

For creating push button we will use QPushButton. This turns the push button into a menu button, which in some styles will produce a small triangle to the right of the button's text.


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

Mybutton001001.jpg

Source Code

The full source code presented in this article is available here File:Button.zip


Related Links

287 page views in the last 30 days.
×