霍亚飞Gitbook教程19篇使用Qt5时出错解决办法

1.查看错误信息,可能函数名和返回值类型void没隔开

2.提示各种类出错的话,在编辑器里点中类的名字,按F1查看帮助文件,然后把这个类需要的头文件都加上。

3.19篇中的程序正常运行用到的所有头文件和程序示例

(添加到.pro文件):

SOURCES += \
    main.cpp \
    myitem.cpp
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
HEADERS += \
    myitem.h


(mytiem.h中)

#ifndef MYITEM_H
#define MYITEM_H
#include <QtWidgets>
#include <QGraphicsItem>
class MyItem : public QGraphicsItem
{
public:
    MyItem();
protected:
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent*event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
    QColor color;
};
#endif // MYITEM_H


(main.cpp)中

#include <QtWidgets>
#include <QApplication>
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include "myitem.h"
int main(int argc,char* argv[ ])
{
   QApplication app(argc,argv);
   QGraphicsScene *scene = new QGraphicsScene;  //场景
   MyItem *item =new MyItem;  //矩形项
   scene->addItem(item);  //项添加到场景
   QGraphicsView *view = new QGraphicsView; //视图
   view->setScene(scene);  //视图关联场景
   view->show();  //显示视图
   return app.exec();

}


myitem.pp中

#include "myitem.h"
#include <QGraphicsSceneMouseEvent>
#include <QApplication>
#include <QDrag>
MyItem::MyItem()
{
   setToolTip("Click and drag me!");
   setCursor(Qt::OpenHandCursor);
   color = QColor(qrand()% 256, qrand()% 256, qrand()% 256);
}
QRectF MyItem::boundingRect() const
{
    qreal penWidth = 1;
    return QRectF(0 - penWidth / 2, 0 -penWidth / 2,
               20 + penWidth, 20 + penWidth);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
    Q_UNUSED(option);  //标明该参数没有使用
    Q_UNUSED(widget);
    painter->setBrush(color);
    painter->drawEllipse(0,0,20,20);
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
   if(event->button() != Qt::LeftButton)
    {
       event->ignore();  //如果不是鼠标左键按下,则忽略该事件
       return;
    }
   setCursor(Qt::ClosedHandCursor); //如果是鼠标左键按下,改变光标形状
}
void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
   if(QLineF(event->screenPos(),event->buttonDownScreenPos(Qt::LeftButton))
       .length() < QApplication::startDragDistance())
{
//如果鼠标按下的点到现在的点的距离小于程序默认的拖动距离,表明没有拖动,则返回
       return;
    }
   QDrag *drag = new QDrag(event->widget()); //为event所在窗口部件新建拖动对象
   QMimeData *mime = new QMimeData; //新建QMimeData对象,它用来存储拖动的数据
   drag->setMimeData(mime); //关联
   mime->setColorData(color);  //放入颜色数据
   QPixmap pix(21,21);  //新建QPixmap对象,它用来重新绘制圆形,在拖动时显示
   pix.fill(Qt::white);
   QPainter painter(&pix);
   paint(&painter,0,0);
   drag->setPixmap(pix);
   drag->setHotSpot(QPoint(10, 15)); //我们让指针指向圆形的(10,15)点
   drag->exec();  //开始拖动
   setCursor(Qt::OpenHandCursor);   //改变光标形状
}
void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
   setCursor(Qt::OpenHandCursor);   //改变光标形状
}


展开阅读全文

没有更多推荐了,返回首页