QT中多个按钮响应同一个槽函数
来源:互联网 发布:淘宝客服话术模板 编辑:程序博客网 时间:2024/06/05 22:47
1.新建7个按钮,前四个按钮分别添加图标和文字,这四个按钮代表供拍照选择的四中状态,上拉三角按钮用来显示和隐藏四种状态按钮,当点击四种状态中的任一种状态按钮时,下面的图标按钮就显示那种状态的图标,并且这四种状态按钮隐藏,此时,点击拍照按钮,就拍下最后被选中的状态
.h文件中的代码如下:
#ifndef MYMENU_H
#define MYMENU_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";
}
}
- QT中多个按钮响应同一个槽函数
- qt 按钮事件不响应
- QT程序登录界面回车按钮响应
- Qt 中单击按钮不响应
- Qt 按钮被遮挡,点击响应
- Qt开发 槽函数定义,以及槽函数二次响应多次响应问题
- 用ON_COMMAND_RANGE实现多个事件响应同一个函数
- Qt 子窗口上控件的槽函数不响应
- 按钮的消息响应--就是设置函数
- ON_COMMAND_RANGE 多个按钮响应一个函数
- ON_COMMAND_RANGE 多个按钮响应一个函数
- Qt中点击按钮如何向槽函数传参数?
- 重写Activity实现多个Activity引用同一个Activity的按钮布局和响应
- Android中同一个button按钮每次点击响应不同的事件
- wxpython中当多个按钮响应同一个事件的时候怎么区分
- Qt中槽函数如何获取按钮QPushButton的按钮文本
- c++中多个线程使用同一个函数
- qt#4信号触发的槽函数到底是哪个线程响应
- 消息队列ipc:ftok,msgget,msgsnd,msgrcv,msgctl
- 【LeetCode】 138. Copy List with Random Pointer C语言
- 深入浅出nodejs学习笔记-前三章 简介、模块机制、异步I/O
- Swift分支语句—switch语句
- python使用代理访问网站
- QT中多个按钮响应同一个槽函数
- leetcode-71 simplify path
- 仿智能指针
- 游戏制作要处理的相关细节
- 什么是P问题、NP问题和NPC问题
- 【Spring】使用Spring发送邮件
- AngularJS路由 $state服务、路由事件、获取路由参数
- ZOJ2208-To and Fro
- TextView 加边框