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. QStyledItemDelegate with QLineEdit
QtWS25 Last Chance

QStyledItemDelegate with QLineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 3.3k Views
  • 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.
  • E Offline
    E Offline
    Elnur_Ismailzada
    wrote on 3 Jul 2018, 22:02 last edited by
    #1

    I want to draw a QLineEdit in the cell of the table.
    I have:

    class ItemDelegate : public QStyledItemDelegate
    {
    public:
    ...
    void paint(QPainter *painter, const QStyleOptionViewItem &option,
                   const QModelIndex &index) const override
    {
            QStyleOptionFrameV2 *panelFrame = new QStyleOptionFrameV2;
            QLineEdit *search = new QLineEdit;
            panelFrame->initFrom(search);
            panelFrame->rect = option.rect;
            panelFrame->state |= QStyle::State_Sunken;
            QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, panelFrame, painter);
    }
    ...
    

    But this code does not work. How can i solve this problem?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 3 Jul 2018, 22:07 last edited by mrjj 7 Mar 2018, 22:09
      #2

      Hi
      What does it draw ?
      In what way does not work ?
      Note: you should delete both panelFrame and search as else you leak.
      ps. did you get code from here ?
      https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-control

      E 2 Replies Last reply 3 Jul 2018, 22:11
      1
      • M mrjj
        3 Jul 2018, 22:07

        Hi
        What does it draw ?
        In what way does not work ?
        Note: you should delete both panelFrame and search as else you leak.
        ps. did you get code from here ?
        https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-control

        E Offline
        E Offline
        Elnur_Ismailzada
        wrote on 3 Jul 2018, 22:11 last edited by
        #3

        @mrjj
        The result of this code: I can not write a text.
        0_1530655794095_7f722725-c980-4229-b35f-89fb8cdb9eef-image.png

        1 Reply Last reply
        0
        • M mrjj
          3 Jul 2018, 22:07

          Hi
          What does it draw ?
          In what way does not work ?
          Note: you should delete both panelFrame and search as else you leak.
          ps. did you get code from here ?
          https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-control

          E Offline
          E Offline
          Elnur_Ismailzada
          wrote on 3 Jul 2018, 22:16 last edited by Elnur_Ismailzada 7 Mar 2018, 22:17
          #4

          @mrjj said in QStyledItemDelegate with QLineEdit:

          ps. did you get code from here ?
          https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-control

          I have seen that. Here I can not write a text too.
          0_1530656018566_5698bb14-8db7-441e-b00e-16eff809a3d2-image.png

          M 1 Reply Last reply 3 Jul 2018, 22:20
          0
          • E Elnur_Ismailzada
            3 Jul 2018, 22:16

            @mrjj said in QStyledItemDelegate with QLineEdit:

            ps. did you get code from here ?
            https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-control

            I have seen that. Here I can not write a text too.
            0_1530656018566_5698bb14-8db7-441e-b00e-16eff809a3d2-image.png

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 3 Jul 2018, 22:20 last edited by
            #5

            @Elnur_Ismailzada
            No, as its painted. ?
            If you want it "alive", you must provide full Delegate with CreateEditor also to make
            a real edit when in edit mode. The paint part if only for non edit mode.

            http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html
            You did also implement
            QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
            const QModelIndex &index) const override;
            void setEditorData(QWidget *editor, const QModelIndex &index) const override;
            ?

            E 1 Reply Last reply 3 Jul 2018, 22:35
            3
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 3 Jul 2018, 22:21 last edited by mrjj 7 Mar 2018, 22:24
              #6

              @Elnur_Ismailzada said in QStyledItemDelegate with QLineEdit:

              I have seen that. Here I can not write a text too.

              You mean it wont draw its text ?
              did you try setting text on search?
              Since its empty as you just create it.

              1 Reply Last reply
              0
              • M mrjj
                3 Jul 2018, 22:20

                @Elnur_Ismailzada
                No, as its painted. ?
                If you want it "alive", you must provide full Delegate with CreateEditor also to make
                a real edit when in edit mode. The paint part if only for non edit mode.

                http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html
                You did also implement
                QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                const QModelIndex &index) const override;
                void setEditorData(QWidget *editor, const QModelIndex &index) const override;
                ?

                E Offline
                E Offline
                Elnur_Ismailzada
                wrote on 3 Jul 2018, 22:35 last edited by Elnur_Ismailzada 7 Mar 2018, 22:36
                #7

                @mrjj said in QStyledItemDelegate with QLineEdit:

                You did also implement
                QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                const QModelIndex &index) const override;
                void setEditorData(QWidget *editor, const QModelIndex &index) const override;

                No. I'll try and let you know. thx

                You mean it wont draw its text ?
                did you try setting text on search?

                Yes and yes.

                M 1 Reply Last reply 4 Jul 2018, 07:19
                0
                • E Elnur_Ismailzada
                  3 Jul 2018, 22:35

                  @mrjj said in QStyledItemDelegate with QLineEdit:

                  You did also implement
                  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                  const QModelIndex &index) const override;
                  void setEditorData(QWidget *editor, const QModelIndex &index) const override;

                  No. I'll try and let you know. thx

                  You mean it wont draw its text ?
                  did you try setting text on search?

                  Yes and yes.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 4 Jul 2018, 07:19 last edited by
                  #8

                  @Elnur_Ismailzada

                  Hi
                  There is one thing i do wonder.
                  The default delegate for the Views is a LineEdit so
                  can i ask why you want to replace it again with delegate ?
                  You want draw extra stuff on it or how come the normal one is not good enough ?

                  E 1 Reply Last reply 4 Jul 2018, 12:44
                  3
                  • M mrjj
                    4 Jul 2018, 07:19

                    @Elnur_Ismailzada

                    Hi
                    There is one thing i do wonder.
                    The default delegate for the Views is a LineEdit so
                    can i ask why you want to replace it again with delegate ?
                    You want draw extra stuff on it or how come the normal one is not good enough ?

                    E Offline
                    E Offline
                    Elnur_Ismailzada
                    wrote on 4 Jul 2018, 12:44 last edited by Elnur_Ismailzada 7 Apr 2018, 12:45
                    #9

                    @mrjj
                    Hi!
                    I want to draw three QLineEdit in a table cell.
                    0_1530708332440_8f144fe9-4be8-4a81-a563-03b58007609c-image.png

                    M 1 Reply Last reply 4 Jul 2018, 18:25
                    0
                    • E Elnur_Ismailzada
                      4 Jul 2018, 12:44

                      @mrjj
                      Hi!
                      I want to draw three QLineEdit in a table cell.
                      0_1530708332440_8f144fe9-4be8-4a81-a563-03b58007609c-image.png

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 4 Jul 2018, 18:25 last edited by
                      #10

                      @Elnur_Ismailzada
                      So you want each cell to have 3 values?
                      Well u can do that with delegate but most of the work will be handling the editor widget.
                      it must then contain 3 Edits so user can select which one to edit and
                      you must handle writing back the data to the model in correct index.

                      1 Reply Last reply
                      1

                      4/10

                      3 Jul 2018, 22:16

                      • Login

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