QT实现点击按钮出现信号

创建QtGuitest13项目文件

添加一个基于QFrame的类,命名为CircleWidget

在CircleWidget.h中

#ifndef CIRCLEWIDGET_H
#define CIRCLEWIDGET_H

#include <QFrame>

class CircleWidget : public QFrame
{
	Q_OBJECT

public:
	CircleWidget(QWidget *parent);
	~CircleWidget();
	void my_flag();
	void Xray_Flag();
private:
	void paintEvent ( QPaintEvent * event );
	bool m_Flag;
	bool xray_flag;
};

#endif // CIRCLEWIDGET_H

在CircleWidget.cpp

 

#include "CircleWidget.h"
#include <QPainter>

CircleWidget::CircleWidget(QWidget *parent)
	: QFrame(parent),m_Flag(false),xray_flag(false)
{

}

CircleWidget::~CircleWidget()
{

}
void CircleWidget::my_flag()
{
	m_Flag = true;
	update();
}
void CircleWidget::Xray_Flag()
{
	xray_flag = true;
	update();
}

void CircleWidget::paintEvent ( QPaintEvent * event )
{
	
   

	if (m_Flag == true)
	{
		QPainter painter(this);
		int width = this->width();
		int height = this->height();
		painter.setPen(QColor(0x00, 0xFF, 0x00));
		painter.setBrush(QBrush(QColor(0x00, 0xFF, 0x00)));
		painter.drawEllipse(height, 0, height, height);
		
	}
	if(m_Flag==false)
	{
		QPainter painter(this);
		int width = this->width();
		int height = this->height();

		painter.setPen(QColor(0xFF, 0x00, 0x00));
		painter.setBrush(QBrush(QColor(0xFF, 0x00, 0x00)));
		painter.drawEllipse(0, 0, height, height);
		
	}
	/*if (xray_flag == true)
	{
		QPainter painter(this);
		int width = this->width();
		int height = this->height();
		painter.setPen(QColor(0x00, 0xFF, 0x00));
		painter.setBrush(QBrush(QColor(0x00, 0xFF, 0x00)));
		painter.drawEllipse(height, 0, height, height);
	}
	if (xray_flag == false)
	{
		QPainter painter(this);
		int width = this->width();
		int height = this->height();

		painter.setPen(QColor(0xFF, 0x00, 0x00));
		painter.setBrush(QBrush(QColor(0xFF, 0x00, 0x00)));
		painter.drawEllipse(0, 0, height, height);
	}*/

	
}


在QtGuitest13.h中

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuitest13.h"

class QtGuitest13 : public QMainWindow
{
	Q_OBJECT

public:
	QtGuitest13(QWidget *parent = Q_NULLPTR);
private slots:
	int OnDraw1();
	int OnXray();
private:
	Ui::QtGuitest13Class ui;
};

对应的cpp中

#include "QtGuitest13.h"

QtGuitest13::QtGuitest13(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	
	connect(ui.btnDraw,SIGNAL(clicked()),this,SLOT(OnDraw1()));
	connect(ui.btnXray, SIGNAL(clicked()), this, SLOT(OnXray()));
	
}
int QtGuitest13::OnDraw1()
{
	ui.frame->my_flag();
	return 0;
}
int QtGuitest13::OnXray()
{
	ui.frame_2->Xray_Flag();
	return 0;
}


main.cpp中

#include "QtGuitest13.h"
#include <QtWidgets/QApplication>
#include "CircleWidget.h"
#include "CircleWidget0.h"
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	//QtGuitest13 w;
	CircleWidget0 w(NULL);
	w.show();
	return a.exec();
}


.ui 中

拖入Frame框架然后promote刚刚自己写的类注意大小写

主要是QtGuitest13.cpp中的最后的那句ui.frame->(***)将Button信号和画图信号进行关联实现点击Button后才有动作

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页