View Full Version : How to click and pop up submenu
shihao
18th March 2010, 07:05
Hello, what I want to do is merely pop up a sub-menu when I click on a button.
connect(mybutton2, SIGNAL(clicked()), this, SLOT(call()));
The code above unable to do so. Does anybody knows how to do it?
Lykurg
18th March 2010, 07:09
Use QPushButton::setMenu() or QWidget::setContextMenuPolicy() or create a custom menu in your slot and call exec with the current mouse position, or or or
shihao
19th March 2010, 05:46
Ha, I feel embrace to paste the code again.
mybutton2->setContextMenuPolicy(Qt::ActionsContextMenu);
After I click the button, still the same ..
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QIcon>
#include <qpixmap.h>
#include <QAbstractButton>
#include <QToolButton>
#include <QStyle>
#include <QtGui>
#include <QApplication>
#include "analogclock.h"
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
public slots:
void call();
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900, 400);
QToolButton * mybutton2 = new QToolButton(this);
mybutton2->setIcon(QIcon("/shihao/qicon/clock.JPG"));
mybutton2->setText("Sample text");
mybutton2->setToolButtonStyle(Qt::ToolButtonIconOnly);
mybutton2->setIconSize(QSize(50,80));
mybutton2->setGeometry(490, 70, 95, 100);
mybutton2->setAutoRaise(1);
mybutton2->setContextMenuPolicy(Qt::ActionsContextMenu);
connect(mybutton2, SIGNAL(clicked()), this, SLOT(call()));
}
void call(){
AnalogClock clock;
clock.show();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
//AnalogClock clock;
//clock.show();
return app.exec();
}
Lykurg
19th March 2010, 07:25
a) context menu is only executed for mouse right click button. And where are your actions???
b) try to use the mentioned setMenu().
shihao
19th March 2010, 08:47
Look at my main program
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//MyWidget widget;
//widget.show();
AnalogClock clock;
clock.show();
return app.exec();
}
Lets' say I uncomment the Analogclock like above, the clock program are able to display.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
//AnalogClock clock;
//clock.show();
return app.exec();
}
On the other hand, I uncomment the widget like above code, the menu page able to displayed.
So my problem is, how to I integrate the menu page with analogclock page, when I click the button of menu page, analogclock only will display.
Analogclock not really a menu right? setmenu is the correct library to be use?
Should I place the setmenu(); in the main function?
I apologize if I ask too silly question, my boss throw the whole qt and ask me to explore alone yet just give me a qt link. I am Headache already....
shihao
19th March 2010, 10:01
hi Lykurg,
How ? Don't have any solution for me? Come on, just a simple job for you to point up the clue for me...
Thanks and regards
Shi Hao
shihao
22nd March 2010, 08:39
Or ... is there anywhere pop up a new window by using QPushbutton? Is the QPushbutton able to add pic, adjust pic just like Qtoolbutton?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.