qt 5.15.2 主窗体事件及绘制功能
显示主窗体效果图如下所示:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFixedWidth(600);
w.setFixedHeight(600);
w.show();
//w.showMaximized(); //最大化显示后,再添加布局部件
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
//
void print(QString msg);
void DrawBorad();
void DrawTipText();
void PrintMsg(int x,int y,QString msg);
void line(int x1,int y1,int x2,int y2);
void circle(int x,int y,int radius);
//qt 绘制事件
void paintEvent(QPaintEvent *event);
//qt mouse event
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
void resizeEvent(QResizeEvent *event);
//
void DrawPiece();
private:
Ui::MainWindow *ui;
bool isMousePressed=false;
QPoint currentPoint;
char Current_piece='O';
//初始化
char Board_piece[3][3]=
{
{'_','_','_'},
{'_','_','o'},
{'_','_','x'}
};
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qpainter.h>
#include <iostream>
#include <QDebug>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QResizeEvent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//mouse move event setup must two line OK
this->setMouseTracking(true);
ui->centralwidget->setMouseTracking(true);
//
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::print(QString msg)
{
qDebug()<<msg;
//std:cout<<msg.toStdString()<<std::endl;
}
void MainWindow::DrawBorad()
{
line(0,200,600,200); //c
line(0,400,600,400); //d
line(200,0,200,600); //a
line(400,0,400,600); //b
}
void MainWindow::line(int x1,int y1,int x2,int y2)
{
QPainter painter(this);
painter.drawLine(x1,y1,x2,y2);
}
void MainWindow::circle(int x,int y,int radius)
{
QPainter painter(this);
painter.drawEllipse(QPoint(x,y),radius,radius);
}
void MainWindow::DrawTipText()
{
QPainter painter(this);
//static TCHAR str[64];
//_stprintf_s(str,_T("当前棋子类型:%c"),Current_piece);
//settextcolor(RGB(255,175,45));
//outtextxy(0,0,str);
QPen pen;
pen.setWidth(2);//设置线宽
pen.setStyle(Qt::SolidLine);//样式
pen.setColor(QColor(255,0,0));//文字颜色
painter.setPen(pen);
QString msg=QString::asprintf("当前棋子类型:%c",Current_piece);
painter.drawText(10, 15, msg);//文本内容
}
void MainWindow::PrintMsg(int x,int y,QString msg)
{
QPainter painter(this);
//static TCHAR str[64];
//_stprintf_s(str,_T("当前棋子类型:%c"),Current_piece);
//settextcolor(RGB(255,175,45));
//outtextxy(0,0,str);
QPen pen;
pen.setWidth(2);//设置线宽
pen.setStyle(Qt::SolidLine);//样式
pen.setColor(QColor(255,45,100));//文字颜色
painter.setPen(pen);
painter.drawText(x, y, msg);//文本内容
}
void MainWindow::DrawPiece()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
switch(this->Board_piece[i][j])
{
case 'O':
case 'o':
this->circle(200*j+100,200*i+100,100);
break;
case 'x':
case 'X':
{
int x=200*j;
int y=200*i;
line(x,y,x+200,y+200); //左对角线(x,y)-(x+100,y+100)
line(x+200,y,x,y+200); //右对角线(x+100,y)-(x,y+100)
}
break;
case '_':
case '-':
break;
}
}
}
}
//总绘制事件
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//反走样
painter.setRenderHint(QPainter::Antialiasing, true);
//画背景图
QString jpgPath="E:\\cwgis_qt\\hsg\\qi_three_son\\images\\china_hdz.jpg";
painter.drawPixmap(0,0, 600, 600, QPixmap(jpgPath));
//定义画笔
QPen pen;
pen.setWidth(2);//设置线宽
pen.setColor(QColor(222,255,55));//颜色
pen.setStyle(Qt::SolidLine);//样式
//画刷
QBrush brush;
brush.setColor(Qt::black);//颜色
brush.setStyle(Qt::SolidPattern);//样式
//设置画笔画刷
painter.setPen(pen);
painter.setBrush(brush);
//画直线
painter.drawLine(50,50,150,50);
painter.drawLine(20,50,250,150 );
//画矩形
painter.drawRect(70,200,100,50);
//画椭圆
painter.drawEllipse(QPoint(270,150),50,80);
//画文字
QFont font;
font.setFamily("MV Boli");//文字字体
font.setPixelSize(20);//文字大小
pen.setColor(QColor(255,55,255));//文字颜色
painter.setFont(font);
painter.setPen(pen);
painter.drawText(200, 300, "Qt Creator 12.0.0 (opensource)");//文本内容
//
this->DrawBorad();
this->DrawPiece();
//
this->DrawTipText();
this->PrintMsg(20,30,jpgPath);
this->PrintMsg(20,40,qApp->applicationDirPath()); //获取可执行文件所在目录
//
{
QString msg=QString::asprintf("%d,%d",this->currentPoint.x(),this->currentPoint.y());
this->PrintMsg(40,580,"(x,y)="+msg);
}
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
this->isMousePressed=true;
print("pressed mouse");
this->update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
this->isMousePressed=false;
print("unpresse mouse");
this->update();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QPoint p=event->pos();
this->currentPoint=p;
qDebug()<<"pos="<<p; //需要引用#include <QMouseEvent>
//QString msg=QString::asprintf("%d,%d",p.x(),p.y());
//this->PrintMsg(40,300,"(x,y)="+msg);
//qDebug()<<msg;
if(this->isMousePressed)
{
QString msg=QString::asprintf("%b",this->isMousePressed);
this->PrintMsg(570,40,"isMousePressed="+msg);
}
else
{
QString msg=QString::asprintf("%b",this->isMousePressed);
this->PrintMsg(570,40,"isMousePressed="+msg);
}
print("moveing mouse");
this->update(); //更新触发重绘事件paintEvent 才能动态显示当前点坐标(x,y)=100,200
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug()<<"key: "<<event->key();
this->update();
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
qDebug()<<"wheel: "<<event->angleDelta();
this->update();
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
qDebug()<<"resize: "<<event->size();
this->update();
}
本blog地址:https://blog.csdn.net/hsg77