0

私はQTの新しい人です。今、質問は私を混乱させます。

メインウィンドウで次のようにコーディングします。

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QGraphicsView *view = new QGraphicsView;
    QGraphicsScene *scene =new QGraphicsScene;
    GraphicsTextItem *item = (GraphicsTextItem*)scene->addText(QString("hello world"));
    item->setPos(100,100);
    scene->addItem(item);
    QGraphicsItem *i = scene->itemAt(120,110);
    view->setScene(scene);
    view->show();
}

クラスGraphicsTextItemはQGraphicsTextItemを継承し、保護されたメソッドmousePressDownは次のように再実装されます。

void GraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"mouseDoubleClickEvent happens";
    QGraphicsTextItem::mouseDoubleClickEvent(event);
}

アプリケーションは正常に動作しますが、GraphicsTextItemオブジェクトをダブルクリックすると、GraphicsTextItemクラスのmouseDoubleClickEventには何も起こりません。

あなたの反応を期待してください!

4

1 に答える 1

2

質問が残っていたので、コードを検索して例を作成しましたが、ここにあります:

#include <QGraphicsTextItem>

class GraphicsTextItem : public QGraphicsTextItem
{
    Q_OBJECT

public:
    GraphicsTextItem(QGraphicsItem * parent = 0);

protected:
    void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event );

};

実装:

#include "graphicstextitem.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>

GraphicsTextItem::GraphicsTextItem(QGraphicsItem * parent)
    :QGraphicsTextItem(parent)
{
    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
}

void GraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if (textInteractionFlags() == Qt::NoTextInteraction)
        setTextInteractionFlags(Qt::TextEditorInteraction);
    QGraphicsItem::mouseDoubleClickEvent(event);
}

景色

#include "mainwindow.h"
#include <QtGui>
#include <QtCore>
#include "graphicstextitem.h"

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
{
    QGraphicsScene * scene = new QGraphicsScene();
    QGraphicsView * view = new QGraphicsView();
    view->setScene(scene);

    GraphicsTextItem * text = new GraphicsTextItem();
    text->setPlainText("Hello world");
    scene->addItem(text);


    text->setPos(100,100);
    text->setFlag(QGraphicsItem::ItemIsMovable);
    setCentralWidget(view);
}

この例では、テキスト QGraphicsTextItem をダブルクリックして操作および変更できます。お役に立てば幸いです。

于 2011-10-10T21:03:47.770 に答える