[Five days] Qt from entry to actual combat: the third day

first day

https://blog.csdn.net/qq_40507857/article/details/125730739

the next day

https://blog.csdn.net/qq_40507857/article/details/125863093

third day

3.1 Custom control encapsulation

  • Add new files - Qt - Designer Interface Classes - (.h, .cpp, .ui)
  • Design QSpinBox and QSlider in .ui
  • Use custom controls in Widget, drag a Widget, click to upgrade to, click to add, click to upgrade
  • Realize the function, change the number, the slider moves accordingly, signal and slot monitoring
  • Provide getNum and setNum external interfaces
  • test interface
    insert image description here

3.2 Mouse events in Qt

  • mouse enter event enterEvent
  • Mouse leave event leaveEvent
  • mouse down
  • mouse release
  • mouse movement
  • ev->x() x coordinate ev->y() y coordinate
  • ev->button() can judge all buttons Qt::LeftButton Qt::RightButton
  • ev-buttons() judge combination button
  • Format String QString("%1 %2").arg(111).arg(222);
//鼠标进入事件
void myLabel::enterEvent(QEvent *event){
    
    
//    qDebug()<<"鼠标进入事件";
}

//鼠标离开事件
void myLabel::leaveEvent(QEvent *event){
    
    
//    qDebug()<<"鼠标离开事件";
}

//鼠标按下
void myLabel::mousePressEvent(QMouseEvent *ev){
    
    
    if(ev->button()==Qt::LeftButton){
    
    
        QString str = QString("鼠标按下 x=%1 y=%2 globalX=%3 globalY=%4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
        qDebug()<<str;
    }
}

//鼠标释放
void myLabel::mouseReleaseEvent(QMouseEvent *ev){
    
    
    qDebug()<<"鼠标释放";
}

//鼠标移动
void myLabel::mouseMoveEvent(QMouseEvent *ev){
    
    
    qDebug()<<"鼠标移动";
}

3.3 Timers

timer 1

  • Use event void timerEvent(QTimerEvent *event)
  • Start timer startTimer(1000) millisecond unit
  • The return value of timerEvent is the unique identifier of the timer, which can be compared with event->timerId

timer 2

  • Use the timer class QTimer
  • Create a timer object QTimer *timer = new QTimer(this)
  • Start the timer timer->start (milliseconds)
  • Every certain milliseconds, send the signal timeout to monitor
  • pause timer->stop

3.4 Event event dispatcher

insert image description here

  • Purpose: used for event distribution, and can also be intercepted (not recommended)
  • bool event(QEvent * e)
  • If the return value is true, the event is processed on behalf of the user and does not need to be distributed downwards
  • e->type()==Mouse press event type
//通过Event事件分发器 拦截 鼠标按下事件
bool myLabel::event(QEvent *e){
    
    
    //如果是鼠标按下事件,在Event事件分发中做拦截操作
    if(e->type()==QEvent::MouseButtonPress){
    
    
        QMouseEvent *ev=static_cast<QMouseEvent *>(e);
        QString str = QString("Event函数:鼠标按下 x=%1 y=%2 globalX=%3 globalY=%4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
        qDebug()<<str;
        return true;//true代表用户自己处理这个事件,不向下分发
    }
    //其他事件 交给父类处理 默认处理
    return QLabel::event(e);
}

3.5 Event filter

insert image description here

  • Before the program distributes events to the event distributor, filters can be used to intercept
  • step:
    • Install event filters for controls
    • Rewrite the eventFilter function

3.6 QPainter

  • Drawing event void paintEvent()
  • Declare a painter object QPainter painter(this) this specifies the drawing device
  • Draw lines, draw circles, draw rectangles, draw text
  • Set brush QPen, set brush width, style
  • Set brush QBrush set brush style
void Widget::paintEvent(QPaintEvent *event){
    
    
    //实例化画家对象 this指定的是绘图设备
    QPainter painter(this);

    //设置画笔
    QPen pen(QColor(255,0,0));

    //设置画笔宽度
    pen.setWidth(3);

    //设置画笔的风格
    pen.setStyle(Qt::DotLine);

    //让画家使用这个笔
    painter.setPen(pen);

    //设置画刷
    QBrush brush(Qt::cyan);
    //让画家使用画刷
    painter.setBrush(brush);

    //画线
    painter.drawLine(QPoint(0,0),QPoint(100,100));

    //画圆(椭圆)
    painter.drawEllipse(QPoint(100,100),50,50);

    //画矩形
    painter.drawRect(QRect(20,20,50,50));

    //画文字
    painter.drawText(QRect(10,200,150,50),"好好学习,天天向上!");
}

insert image description here

  • Anti-aliasing leads to inefficiency
  • Move the painter, save the state, and restore the state

3.7 Drawing devices

  • QPainterDevice drawing device: QPixmap, QImage, QBitmap (black and white), QPicture, QWidget
  • QPixmap optimizes the display for different platforms
  • QImage can access pixels
  • QPicture records and reproduces drawing instructions

3.8 QFile read and write operations on files

  • QFile file(path)
  • Read file file.open (open mode)
  • Read all file.readAll()
  • Read file.readLine() by line
  • Determine whether to read the end of the file file.atEnd()
  • Use the encoding format class to specify the reading format QTextCodeC
  • Write file file.open(QIODevice::writeOnly/Append)
  • file.write(content)
//点击选取文件按钮,弹出文件对话框
    connect(ui->pushButton,&QPushButton::clicked,[=](){
    
    
        QString path = QFileDialog::getOpenFileName(this,"打开文件");

        //将路径放入到lineEdit中
        ui->lineEdit->setText(path);

        //读取内容 放入到textEdit中
        QFile file(path);

        //设置打开方式
        file.open(QIODevice::ReadOnly);

//        QByteArray array=file.readAll();

        QByteArray array;
        while (!file.atEnd()) {
    
    
            array+=file.readLine();//按行读取
        }

        //文件对象关闭
        file.close();

        //进行写文件
        file.open(QIODevice::Append);//用追加方式进行写
        file.write("这是追加的内容!");
        file.close();

insert image description here

//QFileInfo 文件信息类
        QFileInfo info(path);

        qDebug()<<"大小:"<<info.size()<<"后缀:"<<info.suffix()<<"名称:"<<info.fileName()<<"路径:"<<info.filePath();
        qDebug()<<"创建日期:"<<info.created().toString("yyyy-MM-dd hh:mm:ss");
        qDebug()<<"最后修改日期:"<<info.lastModified().toString("yyyy-MM-dd hh:mm:ss");

insert image description here

Guess you like

Origin blog.csdn.net/qq_40507857/article/details/125961644