QT之在多个按钮中任选一个按钮,执行被选中的按钮功能
2017-05-09 10:33
330 查看
1、新建七个按钮其中有四个pushButton,三个toolButton,这四个pushButton用来作为选项按钮,其他三个toolButton分别用来作为弹出,显示所选pushButton按钮和执行所选pushButton按钮。
#ifndef MYMENU_H
#define MYMENU_H
.h文件
#include <QtWidgets/QWidget>
#include "ui_mymenu.h"
#include<QPixmap>
#include<QPushButton>
#include<QToolButton>
#pragma execution_character_set("utf-8")
class myMenu : public QWidget
{
Q_OBJECT
public:
myMenu(QWidget *parent = 0);
~myMenu();
QPushButton *button, *button1, *button2, *button3;
QToolButton *tool1, *tool2,*tool3;
bool flag = false;
int toolflag;
private:
Ui::myMenuClass ui;
public slots:
void chooseMenu();
void choosebtn();
void cameraMenu();
};
#endif // MYMENU_H
.cpp文件
#include "mymenu.h"
#include<QDebug>
myMenu::myMenu(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
button = new QPushButton(this);
QPixmap pixmap;
pixmap.load("images/camera_3d.png");
button->setObjectName("bt0");
button->setIcon(pixmap);
button->setIconSize(pixmap.size());
button->setGeometry(100, 89 ,106, 40);
button->setText(tr("3D"));
button->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal;
pal.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button->setPalette(pal);
button1 = new QPushButton(this);
QPixmap pixmap1;
pixmap1.load("images/camera_axis.png");
button1->setObjectName("bt1");
button1->setIcon(pixmap1);
button1->setIconSize(pixmap1.size());
button1->setGeometry(100, 130, 106, 40);
button1->setText(tr("轴向"));
button1->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal1;
pal1.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button1->setPalette(pal1);
button2 = new QPushButton(this);
QPixmap pixmap2;
pixmap2.load("images/camera_portrait.png");
button2->setObjectName("bt2");
button2->setIcon(pixmap2);
button2->setIconSize(pixmap2.size());
button2->setGeometry(100, 171, 106, 40);
button2->setText(tr("纵向"));
button2->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal2;
pal2.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button2->setPalette(pal2);
button3 = new QPushButton(this);
QPixmap pixmap3;
pixmap3.load("images/camera_data.png");
button3->setObjectName("bt3");
button3->setIcon(pixmap3);
button3->setIconSize(pixmap3.size());
button3->setGeometry(100,212, 106, 40);
button3->setText(tr("数据"));
button3->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal3;
pal3.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button3->setPalette(pal3);
tool1 = new QToolButton(this);
QPixmap pixmap11;
pixmap11.load("images/camera.png");
tool1->setIcon(pixmap11);
tool1->setIconSize(pixmap11.size());
tool1->setGeometry(100, 253, 70, 40);
tool1->setStyleSheet("background-color: rgb(0, 0, 0);");
tool2 = new QToolButton(this);
QPixmap pixmap12;
pixmap12.load("images/button_camera.png");
tool2->setIcon(pixmap12);
tool2->setIconSize(pixmap12.size());
tool2->setGeometry(170, 253, 38, 20);
tool2->setStyleSheet("background-color: rgb(0, 0, 0);");
tool3 = new QToolButton(this);
QPixmap pixmap13;
pixmap13.load("images/camera_axis.png");
tool3->setIcon(pixmap13);
tool3->setIconSize(pixmap13.size());
tool3->setGeometry(170, 273, 38, 20);
tool3->setStyleSheet("background-color: rgb(0, 0, 0);");
button->hide();
button1->hide();
button2->hide();
button3->hide();
connect(button, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(button1, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(button2, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(button3, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(tool2, SIGNAL(clicked()), this, SLOT(chooseMenu()));
connect(tool1, SIGNAL(clicked()), this, SLOT(cameraMenu()));
}
myMenu::~myMenu()
{
}
void myMenu::chooseMenu()
{
if (!flag)
{
button->show();
button1->show();
button2->show();
button3->show();
}
}
void myMenu::choosebtn()
{
QPushButton* btn = qobject_cast<QPushButton*>(sender());
QPixmap pixmap;
if ("bt0"== btn->objectName())
{
pixmap.load("images/camera_3d.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 1;
}
else if ("bt1" == btn->objectName())
{
pixmap.load("images/camera_axis.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 2;
}
else if ("bt2" == btn->objectName())
{
pixmap.load("images/camera_portrait.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 3;
}
else if ("bt3" == btn->objectName())
{
pixmap.load("images/camera_data.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 4;
}
}
void myMenu::cameraMenu()
{
if (toolflag==1)
{
qDebug() << "1";
}
else if (toolflag == 2)
{
qDebug() << "2";
}
else if (toolflag == 3)
{
qDebug() << "3";
}
else if (toolflag == 4)
{
qDebug() << "4";
}
}
#ifndef MYMENU_H
#define MYMENU_H
.h文件
#include <QtWidgets/QWidget>
#include "ui_mymenu.h"
#include<QPixmap>
#include<QPushButton>
#include<QToolButton>
#pragma execution_character_set("utf-8")
class myMenu : public QWidget
{
Q_OBJECT
public:
myMenu(QWidget *parent = 0);
~myMenu();
QPushButton *button, *button1, *button2, *button3;
QToolButton *tool1, *tool2,*tool3;
bool flag = false;
int toolflag;
private:
Ui::myMenuClass ui;
public slots:
void chooseMenu();
void choosebtn();
void cameraMenu();
};
#endif // MYMENU_H
.cpp文件
#include "mymenu.h"
#include<QDebug>
myMenu::myMenu(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
button = new QPushButton(this);
QPixmap pixmap;
pixmap.load("images/camera_3d.png");
button->setObjectName("bt0");
button->setIcon(pixmap);
button->setIconSize(pixmap.size());
button->setGeometry(100, 89 ,106, 40);
button->setText(tr("3D"));
button->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal;
pal.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button->setPalette(pal);
button1 = new QPushButton(this);
QPixmap pixmap1;
pixmap1.load("images/camera_axis.png");
button1->setObjectName("bt1");
button1->setIcon(pixmap1);
button1->setIconSize(pixmap1.size());
button1->setGeometry(100, 130, 106, 40);
button1->setText(tr("轴向"));
button1->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal1;
pal1.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button1->setPalette(pal1);
button2 = new QPushButton(this);
QPixmap pixmap2;
pixmap2.load("images/camera_portrait.png");
button2->setObjectName("bt2");
button2->setIcon(pixmap2);
button2->setIconSize(pixmap2.size());
button2->setGeometry(100, 171, 106, 40);
button2->setText(tr("纵向"));
button2->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal2;
pal2.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button2->setPalette(pal2);
button3 = new QPushButton(this);
QPixmap pixmap3;
pixmap3.load("images/camera_data.png");
button3->setObjectName("bt3");
button3->setIcon(pixmap3);
button3->setIconSize(pixmap3.size());
button3->setGeometry(100,212, 106, 40);
button3->setText(tr("数据"));
button3->setStyleSheet("background-color: rgb(0, 0, 0);");
QPalette pal3;
pal3.setColor(QPalette::ButtonText, QColor(255, 255, 255));
button3->setPalette(pal3);
tool1 = new QToolButton(this);
QPixmap pixmap11;
pixmap11.load("images/camera.png");
tool1->setIcon(pixmap11);
tool1->setIconSize(pixmap11.size());
tool1->setGeometry(100, 253, 70, 40);
tool1->setStyleSheet("background-color: rgb(0, 0, 0);");
tool2 = new QToolButton(this);
QPixmap pixmap12;
pixmap12.load("images/button_camera.png");
tool2->setIcon(pixmap12);
tool2->setIconSize(pixmap12.size());
tool2->setGeometry(170, 253, 38, 20);
tool2->setStyleSheet("background-color: rgb(0, 0, 0);");
tool3 = new QToolButton(this);
QPixmap pixmap13;
pixmap13.load("images/camera_axis.png");
tool3->setIcon(pixmap13);
tool3->setIconSize(pixmap13.size());
tool3->setGeometry(170, 273, 38, 20);
tool3->setStyleSheet("background-color: rgb(0, 0, 0);");
button->hide();
button1->hide();
button2->hide();
button3->hide();
connect(button, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(button1, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(button2, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(button3, SIGNAL(clicked()), this, SLOT(choosebtn()));
connect(tool2, SIGNAL(clicked()), this, SLOT(chooseMenu()));
connect(tool1, SIGNAL(clicked()), this, SLOT(cameraMenu()));
}
myMenu::~myMenu()
{
}
void myMenu::chooseMenu()
{
if (!flag)
{
button->show();
button1->show();
button2->show();
button3->show();
}
}
void myMenu::choosebtn()
{
QPushButton* btn = qobject_cast<QPushButton*>(sender());
QPixmap pixmap;
if ("bt0"== btn->objectName())
{
pixmap.load("images/camera_3d.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 1;
}
else if ("bt1" == btn->objectName())
{
pixmap.load("images/camera_axis.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 2;
}
else if ("bt2" == btn->objectName())
{
pixmap.load("images/camera_portrait.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 3;
}
else if ("bt3" == btn->objectName())
{
pixmap.load("images/camera_data.png");
tool3->setIcon(pixmap);
button->hide();
button1->hide();
button2->hide();
button3->hide();
toolflag = 4;
}
}
void myMenu::cameraMenu()
{
if (toolflag==1)
{
qDebug() << "1";
}
else if (toolflag == 2)
{
qDebug() << "2";
}
else if (toolflag == 3)
{
qDebug() << "3";
}
else if (toolflag == 4)
{
qDebug() << "4";
}
}
相关文章推荐
- 在调试ASP.NET时,有时老发现页面一个按钮的单击事件重复执行的解决办法
- 两个表单一个按钮;一个表单,两个提交按钮。分别执行两个asp文件(asp)
- 为什么刷新页面会触发最近执行过的一个按钮事件?
- 用jquery 绑定一个按钮click事件后,第一次点击后,一切正常,第二次点击,竟然执行两次,以后越来越多
- Qt中同一个线程里,QTimer的timeout()信号相关的槽未执行完,会发生什么?
- 谁知道在ASP.NET 中怎么让一个按钮执行两个CommandName?
- 黄聪:如何在wordpress任何一个页面调用“插入图片”按钮,实现上传图片功能
- 在Qt中如何判断鼠标离开某一操作的时候,在时间差内自动执行某一个操作
- js 功能:敲回车键执行提交按钮
- QT的一个奇怪问题,设置了Qt::Tool后,点击弹出对话框的确定取消按钮,程序直接退出。
- 点击一个按钮后,它会根据不同的情况执行不同的操作,执行客户段的应用程序
- Delphi中窗体的帮助按钮上执行一个自定义的动作
- 设置一个按钮,实现关机功能
- 点击一个按钮后禁用按钮,执行完事件后再启用按钮
- 两台硬件和软件配置完全相同的机器A和B,现在要用系统自带的Copy功能把A上的一个文件,复制到B上。在哪台机器上执行程序,效率更高?
- 对话框完全显示后,马上执行一个按钮的事件
- Qt的一个颜色选取按钮QColorButton
- 做一个翻书页的功能 如命令按钮“上一页”“下一页”
- 点击一个按钮后禁用按钮,执行完事件后再启用按钮
- 在别的程序中建立一个按钮,并执行按钮中的事件