Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Inherit QListView



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



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


  • Qt Champions 2017

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



  • This post is deleted!


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


  • Qt Champions 2017

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



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


  • Moderators

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


  • Lifetime Qt Champion

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



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


  • Moderators

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


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


  • Lifetime Qt Champion

    @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



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


Log in to reply