×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Creating Context Menu from QPushButton

From Nokia Developer Wiki
Jump to: navigation, search
mind_freak (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(35 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{CodeSnippet
+
{{Archived|timestamp=20120213032138|user=[[User:Hamishwillee|&lt;br /&gt;----]]|[[: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.}}
|id=...
+
[[Category:Qt C++ UI]][[Category:UI]][[Category:How To]][[Category:Code Examples]][[Category:MeeGo Harmattan]][[Category:Symbian]]
|platform=S60 3rd Edition, S60 5th Edition
+
{{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}}.
|devices=5800 Xpress Muzic
+
{{ArticleMetaData <!-- v1.2 -->
|category=Qt for S60
+
|sourcecode= [[Media:Button.zip]]
|subcategory= UI
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|creationdate= 27 June 2009
+
|devices= Emulator/ N8
|keywords=QPushButton
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= S60 3rd Edition, S60 5th Edition
 +
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= QPushButton
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090627
 +
|author= [[User:Mind freak]]
 
}}
 
}}
  
[[Category:Qt]][[Category:Qt for S60]][[Category:S60]][[Category:How To]][[Category:Code Examples]]
+
== Class Definition ==
 +
<code cpp-qt>
 +
#include <QMainWindow>
 +
#include "QHBoxLayout"
 +
#include "QPushButton"
  
==Introdction==
+
namespace Ui {
Program shows the menu generated when button is pressed.
+
    class Button;
 +
}
  
==Preconditions==
+
class Button : public QMainWindow
 +
{
 +
    Q_OBJECT
  
* Install Qt for S60 Tower release from here: [http://pepper.troll.no/s60prereleases/ Qt for S60 "Tower" pre-release]
+
public:
* Check this link for installation guide: [http://pepper.troll.no/s60prereleases/doc/install-s60.html How to install the package].
+
    explicit Button(QWidget *parent = 0);
* Download Qt creator IDE:[http://www.qtsoftware.com/downloads QtIDEv4.5]
+
    ~Button();
 +
private:
 +
    QWidget* win;
 +
    QHBoxLayout *lay;
 +
    QMenu *menu;
 +
    QPushButton* but1;
 +
};
  
==Source Code==
+
#endif // BUTTON_H
== Source File==
+
 
<code cpp>
+
</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-qt>
 
#include "button.h"
 
#include "button.h"
#include "ui_button.h"
+
Button::Button(QWidget *parent) :
 +
    QMainWindow(parent)
  
button::button(QWidget *parent)
 
    : QWidget(parent)
 
 
{
 
{
  lay=new QHBoxLayout();
+
 
  menu=new QMenu();
+
      win=new QWidget(this);
  menu->addMenu("Nokia");
+
      win->resize(400,500);
  menu->addMenu("N96");
+
      lay=new QHBoxLayout(this);
  menu->addMenu("N97");
+
      menu=new QMenu(this);
  but1=new QPushButton("Menu");
+
      menu->addMenu("Nokia");
  but1->setMenu(menu);
+
      menu->addMenu("N96");
  lay->addWidget(but1);
+
      menu->addMenu("N97");
  setLayout(lay);
+
      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()
+
Button::~Button()
 
{
 
{
  if(menu)
 
  {
 
      delete menu;
 
  }
 
  if(but1)
 
  {
 
      delete but1;
 
  }
 
  if(lay)
 
  {
 
      delete lay;
 
  }
 
  
 
}
 
}
 
</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:Butmenu.JPG]]
 
  
[[Image:Butmenu1.JPG]]
+
[[File:Mybutton001001.jpg]]
  
 +
==Source Code==
 +
The full source code presented in this article is available here [[File:Button.zip]]
  
 
==Related Links==
 
==Related Links==
[http://wiki.forum.nokia.com/index.php/How_to_use_QPushButton_(Qt_for_S60) How to use QPushButton]
+
* [[How to use QPushButton (Qt)|How to use QPushButton]]
 +
<!-- Translation --> [[pt:Archived:Como criar um Menu, usando um QPushButton]]

Latest revision as of 04:14, 11 October 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

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.

Article Metadata
Code Example
Source file: Media:Button.zip
Tested with
Devices(s): Emulator/ N8
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition
Symbian
Article
Keywords: QPushButton
Created: mind_freak (27 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] 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.


[edit] 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.

[edit] Screenshot

Mybutton001001.jpg

[edit] Source Code

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

[edit] Related Links

This page was last modified on 11 October 2012, at 04:14.
262 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×