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

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 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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
      1
      • mrjjM mrjj

        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 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
        • mrjjM mrjj

          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 last edited by Elnur_Ismailzada
          #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

          mrjjM 1 Reply Last reply
          0
          • E Elnur_Ismailzada

            @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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #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
              • mrjjM mrjj

                @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 last edited by Elnur_Ismailzada
                #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.

                mrjjM 1 Reply Last reply
                0
                • E Elnur_Ismailzada

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  3
                  • mrjjM mrjj

                    @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 last edited by Elnur_Ismailzada
                    #9

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

                    mrjjM 1 Reply Last reply
                    0
                    • E Elnur_Ismailzada

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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