gpt4 book ai didi

c++ - QListWidget 在单击并拖动多个项目时导致段错误

转载 作者:行者123 更新时间:2023-11-28 04:28:28 27 4
gpt4 key购买 nike

我有一个包含多个项目的 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)

最佳答案

看来段错误发生在 UserListWidget::updateStatsPage() 方法中。

鉴于可用信息有限,而且您曾说过,当您将鼠标从最初单击的项目上移开时会出现此问题,我怀疑 this->currentItem() 在方法调用 this->currentItem()->text() 的代码行中返回空指针。当您第一次单击小部件列表项时,我猜测 UserListWidget::updateStatsPage() 方法被调用,并且从 this->currentItem() 返回了一个非空指针。但随后您拖动鼠标,如果将其拖离当前项目,则会生成另一个 itemSelectionChanged() 信号。如果您已将鼠标完全从 QListWidget 上拖开,我想信号会在 this->currentItem() 返回空指针时被调​​用,指示没有被选中。

尝试检查 this->currentItem() 是否为 null,如果不为 null,则只取消引用它。

关于c++ - QListWidget 在单击并拖动多个项目时导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53683855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com