您的位置:首页 > 编程语言 > Qt开发

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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐