简体   繁体   中英

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. 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? 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")

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 () . 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 ) . 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.

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. 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. 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)

<?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

#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

#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

#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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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