Home » QT » QT基础

QT基础

编 辑:Y ┊ 时 间:2018年11月01日 ┊ 访问: 28 次

首先引入头文件

    #include <QApplication>
    #include <QWidget>

    //抽象一个QT的应用程序

    QApplication app(argc, argv);
    
    QWidget
    QWidget w; //QWidget 抽象了窗口

    w.show(); //显示窗口
    
    
    return app.exec();//进入消息循环,等待用户输入
    
    
    virtual void paintEvent(QPaintEvent *);
    //paintEvent(QPaintEvent *)函数是QWidget类中的虚函数,用于ui的绘制,会在多种情况下被其他函数自动调用。
    virtual void mousePressEvent(QMouseEvent *); //鼠标事件
    
    #include <QPainter>  //绘制
    
    QPushButton * _button; //按钮
    _button = new QPushButton("This is button" ,this);  //this表示基层不加表示独立一层
    _button->show();  //如果独立的层使用这个显示
    
    QPoint _ptclick;//位置
    
    #include <QMouseEvent>  //鼠标事件


    void MyWidget::paintEvent(QPaintEvent *)
    {
        //绘制类
        //绘制窗口、打印机、图画、截图
        //QPainter painter(this);//绘制对象是自己
        //painter.drawLine(QPoint(0, 0), QPoint(100, 100));
        QPainter painter(this);
    #if 0
    
        painter.setPen(QColor(255, 0, 0));
        painter.setBrush(QBrush(QColor(255, 255, 0)));
        //QBrush 画刷设置
        //QColor 颜色设置
    
        painter.drawLine(QPoint(0, 0), QPoint(100, 100));
        painter.drawText(200, 200, "Hello QT");
        painter.drawEllipse(QPoint(200,300), 50, 50);
        //drawLine 画线
        //drawText 写文字
        //drawEllipse 画圆
    #endif
        painter.drawEllipse(_ptclick, 30, 30);
    }
    
    //鼠标点击事件
    void MyWidget::mousePressEvent(QMouseEvent *ev)
    {
    //得到鼠标点击的地方
        _ptclick = ev->pos();  //获取位置
    
        //QPainter p(this);
        
        update();//强制程序重新绘制界面
    }



Copyright © 2026 Y 版权所有.网站运行:13年238天21小时23分