Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Typing on a QWidget
Forum Update on Tuesday, May 27th 2025

Typing on a QWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 952 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 10 Jun 2020, 18:57 last edited by mrjj 6 Oct 2020, 18:58
    #2

    Hi
    There is nothing that would allow in-place editing and formatting as far as i know.

    However, using QTextDocument/QTextEdit as a render (aka)

    QTextEdit te("This is a rich text");
    te.resize(100, 100);
    QPixmap pix = QPixmap::grabWidget (&te, te.rect()); // this you draw/update after edit/remove on delete
    
    

    and then a mini list system of "textobjects" x,y,image, Text
    that you paint over what you draw with QPainter

    then override mousepress/add to and loop your list to be able to click them.
    (and pop a dialog )

    that allows you to draw formatted text in various sizes and change its properties.

    This would be a basic solution. In place draw while typing would require a very custom widget.(IMHO)

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Jun 2020, 19:05 last edited by
      #3

      Hi,

      Are you implementing something like QGraphicsTextItem ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 10 Jun 2020, 19:11
      2
      • S SGaist
        10 Jun 2020, 19:05

        Hi,

        Are you implementing something like QGraphicsTextItem ?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 10 Jun 2020, 19:11 last edited by
        #4

        @SGaist
        Good catch ! Actually for the whole Widget :)

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jkwok678
          wrote on 10 Jun 2020, 19:17 last edited by
          #5

          Honestly not entirely sure what the implementation could be, but the ideal thing would be when i click on the screen it just allows me to type there, a bit like how you could on a photo editor like photoshop.

          @mrjj
          I think QTextEdit is one way to do it,but I'm just trying to see if there's a more seemless way of doing it. Perhaps even a flashing line where the next character will be inputted.

          @SGaist
          Possibly, but does it allow me to write in real time and display? I've been using QPainter so far, and the various draw methods. So I was thinking it would probably be just using drawText.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Jun 2020, 19:49 last edited by
            #6

            Yes it does. You can check its implementation.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jkwok678
              wrote on 10 Jun 2020, 20:32 last edited by
              #7
              This post is deleted!
              1 Reply Last reply
              0
              • J Offline
                J Offline
                jkwok678
                wrote on 11 Jun 2020, 12:04 last edited by
                #8

                Would I have to use QGraphicsView for this then?
                Ideally I would like to use it inside a paintEvent method that I have that paints all the other objects and images.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 11 Jun 2020, 18:19 last edited by
                  #9

                  Usually on painting application, you have a dedicated functionality for that. It seems that you want to have everything done by a single class. Is that the case ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jkwok678
                    wrote on 11 Jun 2020, 18:54 last edited by
                    #10

                    I think so, I have a class called canvas, that inherits from QWidget, and in there. I have a method to detect mouse clicks and a paintEvent method. All my painting is done in there.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 11 Jun 2020, 19:05 last edited by
                      #11

                      You might want to consider splitting things a bit. Having one mega class that does everything is usually a bad sign.

                      The Graphics View framework might give you some ideas as well as the plug and paint example.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • J Offline
                        J Offline
                        jkwok678
                        wrote on 11 Jun 2020, 21:24 last edited by jkwok678 6 Nov 2020, 21:24
                        #12

                        Ok, I'll have a look, there are only just the 2 methods for registering mouse clicks and doing the paintevent in my canvas class. With the graphics view framework, does that mean I can still use paintevent and painter.draw___?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jkwok678
                          wrote on 11 Jun 2020, 23:41 last edited by
                          #13
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jkwok678
                            wrote on 13 Jun 2020, 15:51 last edited by
                            #14

                            Is QLineEdit also an option? In terms of typing text on a custom QWidget?

                            M 1 Reply Last reply 14 Jun 2020, 10:56
                            0
                            • J jkwok678
                              13 Jun 2020, 15:51

                              Is QLineEdit also an option? In terms of typing text on a custom QWidget?

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 14 Jun 2020, 10:56 last edited by mrjj
                              #15

                              @jkwok678

                              Yes it could be but not that easy to get working smoothly but yes it works
                              but it was not that easy to place correctly on a "canvas".
                              Also you need to have some sort of Text obejct simply to keep track of Text posistions etc.
                              Else when use click on some painted text, how can you place the lineEdit on top of it ?
                              Also, you need erase area/redraw area pr key stroke to make it "live"

                              I think i would change my "canvas" to be based on QGraphicsItem
                              https://doc.qt.io/qt-5/qgraphicsitem.html
                              Since it has Paint and mouse event function just like QWidget
                              and you can reuse most of the code for canvas.
                              Then you can just use QGraphicsTextItem with your new canvas
                              and get the wanted text editing.

                              That said, you might have some surprises with coordinate system, etc as that is not the same as for
                              QWidgets. So it might take some reading to use the QGraphicXX framework.
                              But it would give you in place edit

                              Try out
                              https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html
                              (its in creator)
                              and see if you think it would be worth the effort.

                              class SimpleItem : public QGraphicsItem
                              {
                              public:
                                  QRectF boundingRect() const override
                                  {
                                      qreal penWidth = 1;
                                      return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                                                    20 + penWidth, 20 + penWidth);
                                  }
                              
                              // like paintEvent for Widgets
                                  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                                             QWidget *widget) override     {
                                      painter->drawRoundedRect(-10, -10, 20, 20, 5, 5); <<< note the coordinates. its NOT 0,0 in top/left but in center. Unlike with QWids. just so you are aware of this.
                                  }
                              };
                              
                              1 Reply Last reply
                              0

                              11/15

                              11 Jun 2020, 19:05

                              • Login

                              • Login or register to search.
                              11 out of 15
                              • First post
                                11/15
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved