单击并拖动多个项目时,QListWidget 导致分段错误

Posted

技术标签:

【中文标题】单击并拖动多个项目时,QListWidget 导致分段错误【英文标题】:QListWidget causing segmentation fault when clicking and dragging across multiple items 【发布时间】:2018-12-08 15:14:28 【问题描述】:

我有一个 QListWidget,其中包含多个项目,每个项目在单击时都像一个按钮响应。我遇到了一个问题,如果您单击一个项目并将鼠标拖动到屏幕上除您单击的项目之外的任何其他位置,那么程序将导致分段错误并崩溃。有谁知道我该如何解决这个问题?

我已经包含了我在下面编写的所有代码,尽管此代码还依赖于我认为我不能在此处发布的专有代码

window.cc

#include "globals.h"

#include <QLabel>
#include <QBoxLayout>
#include <QScrollArea>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>
#include <QMessageBox>

#include "windowheader.h"

namespace

class statisticsTab : public QWidget 
public:
    statisticsTab();
private:
    QGridLayout * layout;
    QLabel * title;
    QLabel * userListTitle;
    QLabel * branchListTitle;
    UserListWidget * userList;
    BranchListWidget * branchList;
    QListWidget * statsPage;
;

inline statisticsTab::statisticsTab() : QWidget() 
    layout = new QGridLayout();
    cur_repo = new GITPP::REPO();

    title = new QLabel("Repository Statistics");
    title->setStyleSheet("QLabel font-weight: bold;");
    layout->addWidget(title, 0, 0, 1, 2);

    userListTitle = new QLabel("Contributors");
    layout->addWidget(userListTitle, 1, 0, 1, 1);

    branchListTitle = new QLabel("Branches");
    layout->addWidget(branchListTitle, 1, 1, 1, 1);

    statsPage = new QListWidget();
    layout->addWidget(statsPage, 3, 0, 1, 2, Qt::AlignTop);
    QListWidgetItem * statsPageDefault = new QListWidgetItem(QString("Click on a contributor or branch to get started!"), 0, 0);
    statsPage->addItem(statsPageDefault);

    userList = new UserListWidget(statsPage);
    layout->addWidget(userList, 2, 0, 1, 1);

    branchList = new BranchListWidget(statsPage);
    layout->addWidget(branchList, 2, 1, 1, 1);

    if(cur_repo != nullptr) 
        GITPP::COMMITS commits = cur_repo->commits();
        std::vector <std::string> contributors;

        for(auto commit : commits) 
            contributors.push_back(commit.author());
        

        std::sort(contributors.begin(), contributors.end());
        contributors.erase(unique(contributors.begin(), contributors.end()), contributors.end());

        for(auto contributor : contributors) 
            QString contributorName = QString::fromStdString(contributor);
            QListWidgetItem * contributorNameItem = new QListWidgetItem(contributorName);
            userList->addItem(contributorNameItem);
        

        GITPP::BRANCHES branches = cur_repo->branches();

        for(auto branch : branches) 
            QListWidgetItem * branchName = new QListWidgetItem(QString::fromStdString(branch.name()), 0, 0);
            branchList->addItem(branchName);
        
     else 
        QListWidgetItem * branchListDefault = new QListWidgetItem(QString("No branches found"), 0, 0);
        branchList->addItem(branchListDefault);

        QListWidgetItem * userListDefault = new QListWidgetItem(QString("No users found"), 0, 0);
        userList->addItem(userListDefault);
    

    setLayout(layout);


INSTALL_TAB(statisticsTab, "Statistics");


windowheader.h

#ifndef WINDOWHEADER_H
#define WINDWOHEADER_H

#include <QListWidget>
#include <string>
#include <limits.h>
#include <unistd.h>
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>

#include "globals.h"

class UserListWidget : public QListWidget 
    Q_OBJECT
public:
    UserListWidget(QListWidget * statsPage);

private slots:
    void updateStatsPage();
private:
    QListWidget * statsPage;
;

inline UserListWidget::UserListWidget(QListWidget * statsPageArg) : QListWidget() 
    connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(updateStatsPage()));
    statsPage = statsPageArg;


inline void UserListWidget::updateStatsPage() 
    GITPP::CONFIG config = cur_repo->config();
    GITPP::COMMITS commits = cur_repo->commits();


    QString statsTitle = QString("Here are some stats about the user ") + this->currentItem()->text();
    QListWidgetItem * statsTitleItem = new QListWidgetItem(statsTitle);
    statsPage->clear();
    statsPage->addItem(statsTitleItem);

    for(auto thing : config) 
        QString statsInfo = QString::fromStdString(thing.name());
        QListWidgetItem * statsInfoItem = new QListWidgetItem(statsInfo);
        statsPage->addItem(statsInfoItem);
    

    selectionModel()->clear();


class BranchListWidget : public QListWidget 
    Q_OBJECT
public:
    BranchListWidget(QListWidget * statsPage);

private slots:
    void updateStatsPage();
private:
    QListWidget * statsPage;
;

inline BranchListWidget::BranchListWidget(QListWidget * statsPageArg) : QListWidget() 
    connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(updateStatsPage()));
    statsPage = statsPageArg;


inline void BranchListWidget::updateStatsPage() 
    GITPP::CONFIG config = cur_repo->config();
    GITPP::COMMITS commits = cur_repo->commits();

    QString statsTitle = QString("Here are some stats about the branch ") + this->currentItem()->text();
    QListWidgetItem * statsTitleItem = new QListWidgetItem(statsTitle);
    statsPage->clear();
    statsPage->addItem(statsTitleItem);

    for(auto thing : config) 
        QString statsInfo = QString::fromStdString(thing.name());
        QListWidgetItem * statsInfoItem = new QListWidgetItem(statsInfo);
        statsPage->addItem(statsInfoItem);
    

    selectionModel()->clear();


#endif

Valgrind 输出:

==9475== Invalid read of size 8
==9475==    at 0x1158B5: UserListWidget::updateStatsPage() (in /home/alexis/Desktop/programming/uni_work/comp_2811/cw2/ui_cw3/2811_gui)
==9475==    by 0x5F5D5E8: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5F5D5E8: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5EE0BE6: QItemSelectionModel::selectionChanged(QItemSelection const&, QItemSelection const&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5EE544A: QItemSelectionModel::emitSelectionChanged(QItemSelection const&, QItemSelection const&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5EE8F91: QItemSelectionModel::select(QItemSelection const&, QFlags<QItemSelectionModel::SelectionFlag>) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x54DF9B4: QListView::setSelection(QRect const&, QFlags<QItemSelectionModel::SelectionFlag>) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x54C6B3E: QAbstractItemView::mouseMoveEvent(QMouseEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x54E6386: QListView::mouseMoveEvent(QMouseEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x52B8277: QWidget::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x53A0A0D: QFrame::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x54C7502: QAbstractItemView::viewportEvent(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==9475== 
==9475== 
==9475== Process terminating with default action of signal 11 (SIGSEGV)
==9475==  Access not within mapped region at address 0x0
==9475==    at 0x1158B5: UserListWidget::updateStatsPage() (in /home/alexis/Desktop/programming/uni_work/comp_2811/cw2/ui_cw3/2811_gui)
==9475==    by 0x5F5D5E8: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5F5D5E8: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5EE0BE6: QItemSelectionModel::selectionChanged(QItemSelection const&, QItemSelection const&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5EE544A: QItemSelectionModel::emitSelectionChanged(QItemSelection const&, QItemSelection const&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x5EE8F91: QItemSelectionModel::select(QItemSelection const&, QFlags<QItemSelectionModel::SelectionFlag>) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.7.1)
==9475==    by 0x54DF9B4: QListView::setSelection(QRect const&, QFlags<QItemSelectionModel::SelectionFlag>) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x54C6B3E: QAbstractItemView::mouseMoveEvent(QMouseEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x54E6386: QListView::mouseMoveEvent(QMouseEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x52B8277: QWidget::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x53A0A0D: QFrame::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)
==9475==    by 0x54C7502: QAbstractItemView::viewportEvent(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.7.1)

【问题讨论】:

@drescherjm 好的,我已经添加了我写的所有代码 您是否尝试过在调试器中运行程序并查看分段错误时的堆栈跟踪?我想堆栈跟踪将证明是有见地的。如果您有堆栈跟踪或可以获得它,请将其添加到您的问题中。谢谢! @LouisLangholtz 我已经添加了 Valgrind 输出,希望对您有所帮助 我相信您在 updateStatsPage 中有一个空指针取消引用。如果selectionModel()-&gt;clear(); 被注释掉,它会崩溃吗? 是的,this-&gt;currentItem()-&gt;text(),我刚刚找到了!我为它添加了一个检查,现在它可以完美运行了!谢谢你:) 【参考方案1】:

看来UserListWidget::updateStatsPage() 方法中出现了分段错误。

鉴于可用信息有限,并且您说当您将鼠标拖离最初单击的项目时会发生此问题,我怀疑 this-&gt;currentItem() 在该行中返回一个空指针方法调用this-&gt;currentItem()-&gt;text() 的代码。当您第一次单击小部件列表项时,我猜想调用 UserListWidget::updateStatsPage() 方法时使用从 this-&gt;currentItem() 返回的非空指针。但是然后你拖动鼠标,如果你把它拖离当前项目,就会产生另一个itemSelectionChanged() 信号。如果您将鼠标完全从QListWidget 上拖开,我想在this-&gt;currentItem() 将返回一个指示未选择任何内容的空指针时会调用该信号。

尝试检查this-&gt;currentItem() 是否为空,如果不为空,则仅取消引用它。

【讨论】:

以上是关于单击并拖动多个项目时,QListWidget 导致分段错误的主要内容,如果未能解决你的问题,请参考以下文章

在PyQt5中的QTreeWidget和QListWidget之间拖动项目?

在 QHeaderView 和 QListWidget 之间拖放列

在 QListWidget 项目上绘制边框使文本在单击时消失

QListWidget 向下滚动列表后拖动

向下钻取 QListWidget

如何在pyqt5 QlistWidget中选择多个项目并打印它们[重复]