简体   繁体   English

图形中的文本在Qt中查看

[英]Text in graphicsView in Qt

I am creating an application in Qt in which I can add different entities in graphics view on click of a button. 我正在Qt中创建一个应用程序,在其中可以单击按钮在图形视图中添加不同的实体。 I want that user can add text in graphics view when he clicks the push button for same. 我希望该用户单击按钮时可以在图形视图中添加文本。 How can I make use of QGraphicsTextItem to do so? 如何使用QGraphicsTextItem这样做? I have different buttons in my application like for drawing line, circle in which user specifies the points for doing so. 我的应用程序中有不同的按钮,例如画线,用户在其中指定点的圆圈。 Similarly I have a button to add text. 同样,我有一个添加文本的按钮。 I have seen we can have the implementation of adding text in graphics view as : 我已经看到我们可以实现在图形视图中添加文本的实现:

addText("hello") addText(“ hello”)

This text is already defined. 该文本已经定义。 I want that text should be entered at run time, it should not be statically or predefined. 我希望该文本应在运行时输入,而不应该是静态的或预定义的。

The information you give is quite sparse. 您提供的信息很少。 From what it sounds like, you should be able to get the QTextDocument associated with the QGraphicsTextItem by calling QGraphicsTextItem::document () . QTextDocument ,您应该能够通过调用QGraphicsTextItem::document ()获得与QGraphicsTextItem关联的QTextDocument If you don't want to do anything fancy, you should be able to set/replace the text of the item by calling QTextDocument::setPlainText ( const QString & text ) . 如果您不想做任何花哨的事情,应该可以通过调用QTextDocument::setPlainText ( const QString & text )来设置/替换项目的QTextDocument::setPlainText ( const QString & text ) If you want to prompt the user to enter text, you can use the static QInputDialog::getText method to get a single QString from the user. 如果要提示用户输入文本,则可以使用静态QInputDialog::getText方法从用户获取单个QString

Does this help? 这有帮助吗? If this doesn't match your use case you might need to provide a bit more context. 如果这与您的用例不符,则可能需要提供更多上下文。

Edit: posting a minimal example on how you can place text in a QGraphicsView/Scene. 编辑:发布有关如何在QGraphicsView / Scene中放置文本的最小示例。 You click the mouse button in a particular location, it will ask you to enter text and then put the text where the mouse click happened. 您在特定位置单击鼠标按钮,它将要求您输入文本,然后将文本放在发生鼠标单击的位置。 Does this help? 这有帮助吗?

Edit 2: adding a more complete yet still dummy example below the first one showing creation of a mainwindow with a custom widget and a dummy button to toggle on/off text insertion. 编辑2:在第一个示例下方添加一个更完整但仍为虚拟的示例,该示例显示了使用自定义小部件和一个用于切换打开/关闭文本插入的虚拟按钮创建的主窗口。 Note that this is certainly not production ready code, it should show the working principle and was not optimized or beautified in any way. 请注意,这当然不是生产准备就绪的代码,它应显示工作原理,并且不以任何方式进行优化或美化。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMouseEvent>
#include <QInputDialog>

class MyGraphicsView: public QGraphicsView
{
public:
  MyGraphicsView( QGraphicsScene *scene, QWidget *parent = 0) : QGraphicsView(scene,parent) {}

public slots:
  void mousePressEvent( QMouseEvent * event );



};

void MyGraphicsView::mousePressEvent( QMouseEvent * event )
{
  const QPoint &pos = event->pos();

  bool ok;
  QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                   tr("Please enter your text"), QLineEdit::Normal,
                                   "Replace with your text", &ok);

  if (!ok || text.isEmpty()) return;

  QGraphicsTextItem *textItem = this->scene()->addText(text);
  textItem->setPos(mapToScene(pos));

}


int main( int argc, char **argv )
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

    MyGraphicsView view( &scene );
    view.show();

    return app.exec();
}

'Complete' Example “完成”示例

mainwindow.ui (created with qt designer) mainwindow.ui (使用qt设计器创建)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>640</width>
    <height>480</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="MyGraphicsView" name="graphicsView"/>
    </item>
    <item row="1" column="0">
     <widget class="QToolButton" name="toolButton">
      <property name="text">
       <string>Add Text</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>640</width>
     <height>27</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>MyGraphicsView</class>
   <extends>QGraphicsView</extends>
   <header location="global">mygraphicsview.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

mainwindow.h 主窗口

#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include "mygraphicsview.h"
#include <QGraphicsScene>
#include "ui_mainwindow.h"
#include <QtCore>

class MyMainWindow: public QMainWindow, private Ui::MainWindow
{
  Q_OBJECT
 public:
  MyMainWindow( QWidget *parent = 0 );

  public slots:
  void on_toolButton_clicked( bool checked ) { graphicsView->setAddText(checked); }


};

#endif // MYMAINWINDOW_H                   

mainwindow.cpp 主窗口

#include "mainwindow.h"

MyMainWindow::MyMainWindow( QWidget * ) {
  setupUi(this);
  QGraphicsScene *scene = new QGraphicsScene;
  scene->setSceneRect( -100.0, -100.0, 200.0, 200.0 );
  toolButton->setCheckable(true);
  graphicsView->setScene(scene);

}

mygraphicsview.h mygraphicsview.h

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QGraphicsScene>

class MyGraphicsView: public QGraphicsView
{
  Q_OBJECT
public:
  MyGraphicsView( QWidget *parent = 0) : QGraphicsView(parent), addText(false) {}
  void setAddText(bool state) {addText = state;}
  public slots:
  void mousePressEvent( QMouseEvent * event );

 private:
  bool addText;

};

#endif // MYGRAPHICSVIEW_H         

mygraphicsview.cpp mygraphicsview.cpp

#include "mygraphicsview.h"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMouseEvent>
#include <QInputDialog>
#include <QGraphicsTextItem>




void MyGraphicsView::mousePressEvent( QMouseEvent * event )
{
  if( ! addText) return;

  const QPoint &pos = event->pos();
  bool ok;
  QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                   tr("Please enter your text"), QLineEdit::Normal,
                                   "Replace with your text", &ok);

  if ( !ok || text.isEmpty()) return;

  QGraphicsTextItem *textItem = this->scene()->addText(text);
  textItem->setPos(mapToScene(pos));

}

main.cpp main.cpp

#include "mainwindow.h"
#include "mygraphicsview.h"
#include <QApplication>


int main( int argc, char **argv )
{
    QApplication app(argc, argv);
    MyMainWindow m;
    m.show();
    return app.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM