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";
}
}


0 0
原创粉丝点击