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. Inherit QListView
Qt 6.11 is out! See what's new in the release blog

Inherit QListView

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.9k 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.
  • G Offline
    G Offline
    GuYan
    wrote on last edited by
    #3
    This post is deleted!
    1 Reply Last reply
    0
    • dheerendraD dheerendra

      It cannot work means what is happening ? Why do you want to inherit QListView ? What are you trying to change ?

      G Offline
      G Offline
      GuYan
      wrote on last edited by
      #4

      @dheerendra
      I want to reimplement the paintEvent function and paint a background shape for the list, so I inherit QListView. But there 's no view in the wedget when I run the project.
      And even if I changed the pointer's type and the view showed, the paintEvent function didn't triggered...So I wonder how can I solve this problem...

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        wrote on last edited by
        #5

        Can you try using the Stylesheet for setting the background color ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        https://www.pthinks.com

        G 1 Reply Last reply
        0
        • dheerendraD dheerendra

          Can you try using the Stylesheet for setting the background color ?

          G Offline
          G Offline
          GuYan
          wrote on last edited by
          #6

          @dheerendra
          Actually I want to not only change the background color but also draw some lines and points on it so Stylesheet can't meet my equirements.

          1 Reply Last reply
          0
          • G GuYan

            I want to inherit the QListView class and reimplement the paintEvent function.

            class boardView : public QListView
            {
            Q_OBJECT
            public:
            boardView(QWidget *parent = nullptr);
            protected:
            void paintEvent(QPaintEvent *e);
            };

            But when I want to creat a boardView, it just can't work.

            boardView *boardView1 =new boardView(this);
            boardView1->setObjectName(QString::fromUtf8("boardView"));
            boardView1->setGeometry(QRect(145, 10, 250, 200));

            So I created a QListView pointer and changed it into boardView type.

            boardView *boardView1 =(boardView *)new boardView(this);

            Then it showed in my widget. I really want to know why...

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #7

            @GuYan do you call the base class constructor inside your cpp file?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            G 1 Reply Last reply
            0
            • G GuYan

              I want to inherit the QListView class and reimplement the paintEvent function.

              class boardView : public QListView
              {
              Q_OBJECT
              public:
              boardView(QWidget *parent = nullptr);
              protected:
              void paintEvent(QPaintEvent *e);
              };

              But when I want to creat a boardView, it just can't work.

              boardView *boardView1 =new boardView(this);
              boardView1->setObjectName(QString::fromUtf8("boardView"));
              boardView1->setGeometry(QRect(145, 10, 250, 200));

              So I created a QListView pointer and changed it into boardView type.

              boardView *boardView1 =(boardView *)new boardView(this);

              Then it showed in my widget. I really want to know why...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @GuYan Please add "override" keyword to paintEvent:

              class boardView : public QListView
              {
              Q_OBJECT
              public:
              boardView(QWidget *parent = nullptr);
              protected:
              void paintEvent(QPaintEvent *e) override;
              };
              

              And please show the content of your paintEvent implementation.
              Did you actually call show() on boardView?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              G 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @GuYan do you call the base class constructor inside your cpp file?

                G Offline
                G Offline
                GuYan
                wrote on last edited by
                #9

                @J.Hilk
                Yes, I call the boardView class constructor in the mainwindow class constructor... Is there any problem? Because when I create a QListView in the same way, it can show on the mainwindow.

                J.HilkJ 1 Reply Last reply
                0
                • G GuYan

                  @J.Hilk
                  Yes, I call the boardView class constructor in the mainwindow class constructor... Is there any problem? Because when I create a QListView in the same way, it can show on the mainwindow.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #10

                  @GuYan

                  works perfectly fine for me

                  #ifndef MYLISTVIEW_H
                  #define MYLISTVIEW_H
                  
                  #include <QListView>
                  #include <QDebug>
                  
                  class myListView : public QListView
                  {
                      Q_OBJECT
                  
                  public:
                      explicit myListView(QWidget *parent = nullptr) : QListView(parent){}
                  
                  protected:
                      virtual void paintEvent(QPaintEvent *)override{
                          qDebug() <<Q_FUNC_INFO;
                      }
                  };
                  
                  #endif // MYLISTVIEW_H
                  
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                  
                      myListView mlw;
                      mlw.show();
                  
                  
                      return a.exec();
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @GuYan Please add "override" keyword to paintEvent:

                    class boardView : public QListView
                    {
                    Q_OBJECT
                    public:
                    boardView(QWidget *parent = nullptr);
                    protected:
                    void paintEvent(QPaintEvent *e) override;
                    };
                    

                    And please show the content of your paintEvent implementation.
                    Did you actually call show() on boardView?

                    G Offline
                    G Offline
                    GuYan
                    wrote on last edited by
                    #11

                    @jsulm
                    I have added override keyword now but nothing changed.
                    If I call the show() function, the view will show on another window, but actually I want it to show in the mainwindow.
                    The painEvent function is:
                    void boardView::paintEvent(QPaintEvent *e)
                    {
                    QPainter painter(this->viewport());
                    painter.save();

                    float W = this -> width(); 
                    float H = this -> height();
                    
                    QPoint LeftTopPoint, RightBottomPoint;
                    LeftTopPoint = QPoint((W - H * 7 / 8) / 2, (H - H * 7 / 8) / 2);
                    RightBottomPoint = QPoint((W - H * 7 / 8) / 2 + H * 7 / 8, (H - H * 7 / 8) / 2 + H * 7 / 8); 
                    
                    QRect rect(LeftTopPoint, RightBottomPoint);
                    
                    QVector<QLine> Lines;
                    for(int i = 1; i < 14; i++)
                    {
                        Lines.append(QLine(rect.topLeft().x() + rect.width() / 14.0f * i, rect.topLeft().y(), \
                                           rect.bottomLeft().x() + rect.width() / 14.0f * i,  rect.bottomLeft().y())); 
                        Lines.append(QLine(rect.topLeft().x(), rect.topLeft().y() + rect.height() / 14.0f * i, \
                                           rect.topRight().x(),  rect.topRight().y() + rect.height() / 14.0f * i));
                    }
                    
                    painter.drawRect(rect); 
                    painter.drawLines(Lines); 
                    
                    painter.restore();
                    

                    }

                    //your code here

                    jsulmJ 1 Reply Last reply
                    0
                    • G GuYan

                      @jsulm
                      I have added override keyword now but nothing changed.
                      If I call the show() function, the view will show on another window, but actually I want it to show in the mainwindow.
                      The painEvent function is:
                      void boardView::paintEvent(QPaintEvent *e)
                      {
                      QPainter painter(this->viewport());
                      painter.save();

                      float W = this -> width(); 
                      float H = this -> height();
                      
                      QPoint LeftTopPoint, RightBottomPoint;
                      LeftTopPoint = QPoint((W - H * 7 / 8) / 2, (H - H * 7 / 8) / 2);
                      RightBottomPoint = QPoint((W - H * 7 / 8) / 2 + H * 7 / 8, (H - H * 7 / 8) / 2 + H * 7 / 8); 
                      
                      QRect rect(LeftTopPoint, RightBottomPoint);
                      
                      QVector<QLine> Lines;
                      for(int i = 1; i < 14; i++)
                      {
                          Lines.append(QLine(rect.topLeft().x() + rect.width() / 14.0f * i, rect.topLeft().y(), \
                                             rect.bottomLeft().x() + rect.width() / 14.0f * i,  rect.bottomLeft().y())); 
                          Lines.append(QLine(rect.topLeft().x(), rect.topLeft().y() + rect.height() / 14.0f * i, \
                                             rect.topRight().x(),  rect.topRight().y() + rect.height() / 14.0f * i));
                      }
                      
                      painter.drawRect(rect); 
                      painter.drawLines(Lines); 
                      
                      painter.restore();
                      

                      }

                      //your code here

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @GuYan What is "this" in this line

                      boardView *boardView1 =new boardView(this);
                      

                      ?
                      If you want your widget to be shown in your main window you need to add it to the central widget of the main window. See https://stackoverflow.com/questions/9290767/adding-child-in-qmainwindow

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      G 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @GuYan What is "this" in this line

                        boardView *boardView1 =new boardView(this);
                        

                        ?
                        If you want your widget to be shown in your main window you need to add it to the central widget of the main window. See https://stackoverflow.com/questions/9290767/adding-child-in-qmainwindow

                        G Offline
                        G Offline
                        GuYan
                        wrote on last edited by
                        #13

                        @jsulm
                        “this” is the pointer of the mainwindow.
                        I have tried to use layout and it finally successed. Thanks a lot!

                        1 Reply Last reply
                        0

                        • Login

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