【发布时间】:2015-08-10 11:19:03
【问题描述】:
我在 VS2010SP1 上使用 Qt5.5。我在停靠小部件中有一个 QCheckBox,它控制同一停靠小部件中的 QTableView(提示为 ConsoleWindowView)是否自动滚动。
问题一:
当我将 QCheckBox::stateChanged 信号连接到 QTableView 的插槽 (autoScroll) 时,一切似乎都很好,除非我更改 QCheckBox 的状态并退出。退出时出现错误:
调试错误!程序:QtTests.exe 检测到堆损坏:之后 0x02B63850 处的普通块 (#4613)。 CRT 检测到应用程序 堆缓冲区结束后写入内存。
如果我没有激活QCheckBox就退出,退出是正常的。
问题 2:
认为这可能与销毁顺序有关,我尝试在 QTableView 析构函数中“记住”连接和断开连接,但是当我尝试将 QObject::connect 的返回值分配给 QMetaObject::Connection 成员变量时,我得到了:
QtTests.exe 中 0x669859c2 处未处理的异常:0xC0000005:访问 违规写入位置0xabababc7
代码如下,后面是相关的ui文件。
崩溃的区域在 Gui.h 中,方法 connectControls。对于问题1,配置为:
QObject::connect(AutoScrollCheckBox, &QCheckBox::stateChanged, this, &ConsoleWindowView::autoScroll); // CRASH1: Debug Error! Program: QtTests.exe HEAP CORRUPTION DETECTED: after Normal block (#4613) at 0x02B63850. CRT detected that the application wrote to memory after end of heap buffer.
//QMetaObject::Connection NewConnection = QObject::connect(AutoScrollCheckBox, &QCheckBox::stateChanged, this, &ConsoleWindowView::autoScroll);
//AutoScrollConnection = NewConnection; // CRASH2: Unhandled exception at 0x669859c2 in QtTests.exe: 0xC0000005: Access violation writing location 0xabababc7
对于问题2,配置为:
//QObject::connect(AutoScrollCheckBox, &QCheckBox::stateChanged, this, &ConsoleWindowView::autoScroll); // CRASH1: Debug Error! Program: QtTests.exe HEAP CORRUPTION DETECTED: after Normal block (#4613) at 0x02B63850. CRT detected that the application wrote to memory after end of heap buffer.
QMetaObject::Connection NewConnection = QObject::connect(AutoScrollCheckBox, &QCheckBox::stateChanged, this, &ConsoleWindowView::autoScroll);
AutoScrollConnection = NewConnection; // CRASH2: Unhandled exception at 0x669859c2 in QtTests.exe: 0xC0000005: Access violation writing location 0xabababc7
main.cpp
#include <QtWidgets/QApplication>
#include "Gui.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Gui w;
w.show();
return a.exec();
}
Gui.h
#ifndef QTTESTS_H
#define QTTESTS_H
#include <iostream>
#include <QTimer>
#include <QHeaderView>
#include <QCheckBox>
#include <QObject>
#include "ui_Gui.h"
class ConsoleWindowModelClass : public QAbstractTableModel
{
Q_OBJECT
public:
ConsoleWindowModelClass(QObject *parent)
: QAbstractTableModel(parent)
, RowCount(0)
{
//
ControllerTimer = new QTimer(this);
connect(ControllerTimer, SIGNAL(timeout()), this, SLOT(updateController()));
ControllerTimer->start(2000);
}
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
return RowCount;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
return 2;
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE
{
if (role == Qt::DisplayRole)
{
return QString("Row%1, Column%2")
.arg(index.row() + 1)
.arg(index.column() +1);
}
return QVariant();
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE
{
if (orientation == Qt::Horizontal)
{
if (role == Qt::DisplayRole)
{
switch(section)
{
case 0:
return "Stamp";
break;
case 1:
return "Text";
break;
default:
return QString("Column %1").arg(section + 1);
break;
}
}
}
else if (orientation == Qt::Vertical)
{
if (role == Qt::DisplayRole)
{
return QString("%1").arg(section + 1);
}
}
return QVariant();
}
protected:
unsigned int RowCount;
QTimer *ControllerTimer;
private slots:
// GUI triggered
// Timer triggered
void updateController()
{
beginInsertRows(QModelIndex(), RowCount, RowCount);
RowCount++;
endInsertRows();
}
};
class ConsoleWindowView : public QTableView
{
Q_OBJECT
public:
ConsoleWindowView(QWidget *parent = 0)
: QTableView(parent)
, AutoScroll(true)
{
}
virtual ~ConsoleWindowView()
{
//disconnect(AutoScrollConnection);
}
void setTheModel(QAbstractItemModel *TheModel)
{
QTableView::setModel(TheModel);
connect(model(), &QAbstractItemModel::rowsInserted, this, &ConsoleWindowView::modelRowsInserted);
}
void connectControls(QCheckBox *AutoScrollCheckBox, QCheckBox *ReverseList, QPushButton *MarkerA, QPushButton *MarkerB, QPushButton *MarkerC, QPushButton *MarkerD)
{
try
{
//QObject::connect(AutoScrollCheckBox, &QCheckBox::stateChanged, this, &ConsoleWindowView::autoScroll); // CRASH1: Debug Error! Program: QtTests.exe HEAP CORRUPTION DETECTED: after Normal block (#4613) at 0x02B63850. CRT detected that the application wrote to memory after end of heap buffer.
QMetaObject::Connection NewConnection = QObject::connect(AutoScrollCheckBox, &QCheckBox::stateChanged, this, &ConsoleWindowView::autoScroll);
AutoScrollConnection = NewConnection; // CRASH2: Unhandled exception at 0x669859c2 in QtTests.exe: 0xC0000005: Access violation writing location 0xabababc7
}
catch (const std::exception &Exception)
{
std::cerr << "ConsoleWindowView::connectControls: " << Exception.what() << std::endl;
}
}
public slots:
void modelRowsInserted(const QModelIndex & parent, int start, int end)
{
if (model() != nullptr)
{
if (AutoScroll)
{
scrollTo(model()->index(start, 0));
}
}
}
void autoScroll(int State)
{
if (State == 0)
{
AutoScroll = false;
}
else
{
AutoScroll = true;
}
}
signals:
protected:
bool AutoScroll;
QMetaObject::Connection AutoScrollConnection;
private:
};
class Gui : public QMainWindow
{
Q_OBJECT
public:
Gui(QWidget *parent = 0);
~Gui();
protected:
ConsoleWindowModelClass *ConsoleWindowModel;
private:
Ui::Gui ui;
void createConsoleWindow();
//
private slots:
// GUI triggered
};
#endif // QTTESTS_H
Gui.cpp
#include <iostream>
#include <QTimer>
#include <QHeaderView>
#include <QCheckBox>
#include <QObject>
// Qt model handling the BusSimLowRateTx message from BusSim to DataSwitch.
#include <QAbstractTableModel>
#include "Gui.h"
Gui::Gui(QWidget *parent)
: QMainWindow(parent)
{
// create UI
ui.setupUi(this);
//
ui.actionExit->setShortcuts(QKeySequence::Quit);
ui.actionExit->setStatusTip(tr("Exit the application"));
connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));
// create models
ConsoleWindowModel = new ConsoleWindowModelClass(this); // should be deleted as part of QMainWindow destructor, by specifying this as the parent.
//
// connect ConsoleWindowModel to the View(s)
createConsoleWindow();
}
Gui::~Gui()
{
// ConsoleWindowModel - should be deleted as part of QMainWindow destructor.
//ConsoleWindowView *View = static_cast<ConsoleWindowView *>(ui.ConsoleOutputTable);
//disconnect(ui.AutoScroll, &QCheckBox::stateChanged, View, &ConsoleWindowView::autoScroll);
}
void Gui::createConsoleWindow()
{
ConsoleWindowView *View = static_cast<ConsoleWindowView *>(ui.ConsoleOutputTable);
View->setTheModel(ConsoleWindowModel);
for (int c = 1; c < View->horizontalHeader()->count(); ++c)
{
View->horizontalHeader()->setSectionResizeMode(c, QHeaderView::Stretch);
}
// connect the other ConsoleWindow Controls to the model
View->connectControls(ui.AutoScroll, ui.NewestAtTop, ui.MarkerA, ui.MarkerB, ui.MarkerC, ui.MarkerD);
}
Gui.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Gui</class>
<widget class="QMainWindow" name="Gui">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>593</width>
<height>652</height>
</rect>
</property>
<property name="windowTitle">
<string>Gui Tests</string>
</property>
<widget class="QWidget" name="centralwidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout_7"/>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>593</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuView">
<property name="title">
<string>View</string>
</property>
</widget>
<addaction name="menuFile"/>
<addaction name="menuView"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QDockWidget" name="ConsoleOutput">
<property name="windowTitle">
<string>Console Output</string>
</property>
<attribute name="dockWidgetArea">
<number>8</number>
</attribute>
<widget class="QWidget" name="ConsoleOutputDock">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="AutoScroll">
<property name="text">
<string>Auto Scroll</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="NewestAtTop">
<property name="text">
<string>Newest At Top</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="MarkerA">
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 0, 0);</string>
</property>
<property name="text">
<string>Marker A</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="MarkerB">
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 0);</string>
</property>
<property name="text">
<string>Marker B</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="MarkerC">
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 0);</string>
</property>
<property name="text">
<string>Marker C</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="MarkerD">
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 255);
color: rgb(255, 255, 255);</string>
</property>
<property name="text">
<string>Marker D</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QTableView" name="ConsoleOutputTable"/>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
<action name="actionECIO_Outputs">
<property name="text">
<string>Enlightenment Monitor</string>
</property>
</action>
<action name="actionECIO_Inputs">
<property name="text">
<string>Enlightenment Control</string>
</property>
</action>
<action name="actionSHM_Control">
<property name="text">
<string>SHM Input</string>
</property>
</action>
<action name="actionSHM_Output">
<property name="text">
<string>SHM Output</string>
</property>
</action>
<zorder>ConsoleOutput</zorder>
</widget>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
QObject是一个设计合理的 C++ 类,它始终可以安全地破坏。 在任何情况下您都不需要仅仅因为您碰巧破坏了所涉及的对象而手动断开连接。您的代码还有其他问题,但添加这样的“变通办法”并没有任何帮助。 -
我怀疑以前从未有过这种情况。我认为控件和 QTableView 之间的关系是错误的,但我看不到如何修复它。
标签: c++ qt visual-c++ qt5