- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 VS2010SP1 上使用 Qt5.5。我在停靠小部件中有一个 QCheckBox,它控制同一停靠小部件中的 QTableView(提示为 ConsoleWindowView)是否自动滚动。
问题一:
当我将 QCheckBox::stateChanged 信号连接到 QTableView 的插槽(autoScroll)时,一切似乎都很好,除非我更改 QCheckBox 的状态并退出。在退出时我收到错误:
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.
Unhandled exception at 0x669859c2 in QtTests.exe: 0xC0000005: Access violation writing location 0xabababc7
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
//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
#include <QtWidgets/QApplication>
#include "Gui.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Gui w;
w.show();
return a.exec();
}
#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
#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);
}
<?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>
最佳答案
您正在静态转换 QTableView
的实例至ConsoleWindowView
.它不起作用,因为它不是 ConsoleWindowView
.你期待什么?你为什么要static_cast
它?
如果您不确定静态转换是否有效,请使用 qobject_cast
.它会失败,产生一个 nullptr 结果,你会知道你必须停下来弄清楚为什么你没有得到正确的类实例。你不是因为你的 UI 文件被破坏了——你必须在 UI 文件中有正确类型的实例。
修复您的 UI 文件以实例化正确的类:
ConsoleWindowView
在升级类名中:。 gui.h
在头文件中:. QTimer
那里的成员 - 使用指针并通过间接引用和额外的堆分配过早地悲观有什么意义?让您的生活轻松。 autoScroll
的属性。 .你把一个新属性(property)的同名弄乱了,让每个人的生活都变得糟糕透顶。将其命名为其他名称,例如 scrollToNewest
. setModel
是一种虚方法。覆盖它,不要创建自己的。 signals
和 slots
仅当信号和/或插槽在逻辑上相关并且其中有很多时才应使用节宏 - 太多了,使用 Q_SIGNAL
或 Q_SLOT
前缀会有点多。在大多数情况下,您有少量信号/插槽,您应该使用前缀。然后,您可以轻松地将信号和槽声明与其他相关方法分组,以便声明分组遵循函数,而不是实现细节。 Q_SLOT
前缀,任何方法都可以是带有新 connect
的插槽句法。您只需要 Q_SLOT
或 Q_INVOKABLE
如果您希望方法的元数据在运行时可用,则为前缀。 ConsoleWindowView
类应该看起来:
class ConsoleWindowView : public QTableView
{
Q_OBJECT
Q_PROPERTY(bool scrollToNewest READ scrollToNewest WRITE setScrollToNewest)
bool m_scrollToNewest;
void modelRowsInserted(const QModelIndex &, int start, int end) {
Q_UNUSED(end)
if (model() && m_scrollToNewest) scrollTo(model()->index(start, 0));
}
public:
ConsoleWindowView(QWidget *parent = 0) : QTableView(parent), m_scrollToNewest(true)
{}
void setModel(QAbstractItemModel *TheModel) Q_DECL_OVERRIDE
{
QTableView::setModel(TheModel);
connect(model(), &QAbstractItemModel::rowsInserted, this, &ConsoleWindowView::modelRowsInserted);
}
Q_SLOT void setScrollToNewest(bool s) { m_scrollToNewest = s; }
bool scrollToNewest() const { return m_scrollToNewest; }
};
Gui
类,在将控制台 View 与用于调整它的具体控件解耦之后:
class Gui : public QMainWindow
{
Q_OBJECT
Ui::Gui ui;
ConsoleWindowModel consoleWindowModel;
void connectConsoleWindow() {
auto view = ui.ConsoleOutputTable;
view->setModel(&consoleWindowModel);
for (int c = 1; c < view->horizontalHeader()->count(); ++c)
view->horizontalHeader()->setSectionResizeMode(c, QHeaderView::Stretch);
connect(ui.AutoScroll, &QCheckBox::toggled, view, &ConsoleWindowView::setScrollToNewest);
}
public:
Gui(QWidget *parent = 0) : QMainWindow(parent) {
ui.setupUi(this);
ui.actionExit->setShortcuts(QKeySequence::Quit);
ui.actionExit->setStatusTip(tr("Exit the application"));
connect(ui.actionExit, &QAction::triggered, this, &QWidget::close);
connectConsoleWindow();
}
};
connectConsoleWindow
命名为方法根据它的作用。您已将其命名为
错了如
createConsoleWindow
然后
评论 关于它的行为——也就是说,它不创造任何东西,而只是连接东西。
这是一个可怕的反模式,必须被视为即时代码审查失败 .在可行的情况下,根据他们所做的事情命名(在此处)。使代码自记录。
class ConsoleWindowModel : public QAbstractTableModel
{
Q_OBJECT
unsigned int m_rowCount;
QTimer m_controllerTimer;
void updateController()
{
beginInsertRows(QModelIndex(), m_rowCount, m_rowCount);
m_rowCount++;
endInsertRows();
}
public:
ConsoleWindowModel(QObject *parent = 0)
: QAbstractTableModel(parent)
, m_rowCount(0) {
connect(&m_controllerTimer, &QTimer::timeout, this, &ConsoleWindowModel::updateController);
m_controllerTimer.start(2000);
}
[...]
关于c++ - Qt5 连接导致 Unhandled Exception 或 HEAP CORRUPTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918194/
我正在使用 Test Cafe 中使用 Resemble.JS 库的函数来比较实际屏幕截图和基本屏幕截图。在我的夹具中,我有两个测试,由于屏幕截图不匹配,两个测试都应该在报告中失败,但唯一的第一个测试
这个问题在这里已经有了答案: What does IndexOutofRangeException mean? (3 个答案) 关闭 9 年前。 string Query = "SELECT [AA
错误日志: System.TypeInitializationException was unhandled Message="The type initializer for 'MaxDavid
使用 Python 2.7 和 Django 1.10.4,我尝试将我的应用程序部署到 pythonanywhere,但我不断收到此错误。 错误日志 wsgi.py import os import
我有一个名为 Form1 的表单: 有一个 ComboBox 和一个 TextBox,当我从 ComboBox 中选择 US$ 时,它必须从数据库中检索数据并显示TextBox 中的 150。 这是我
我正在尝试制作 slider 益智游戏,但在我的 form1 中调用 myBoard.paint(e.Graphics) 时,我不断收到错误“NullReferenceException 未处理”。请
我目前正在开发一个数据库,用于跟踪停在 parking 场的车辆。但是,我在将新记录保存到数据库表时遇到了一些问题。现在我对编程还很陌生,所以我无法理解我收到的错误。我试过查找它并发现了类似的情况,但
我编写了一个 Android 应用程序 (4.4.2),它在大多数时间都能正确连接/断开 BLE 外围设备。但是,每隔一段时间我就会在 Bluetootgatt.java onClientConnec
当我尝试运行 Facebook C# SDK 附带的 WP7 示例应用程序时,我收到此异常: File or assembly name 'Microsoft.Contracts, Version=1
我正在尝试学习如何在 Scala 中使用 Try with 进行理解。 在下面的示例代码中( result1 ), 如果 for comprehension 中的最后一条语句抛出未处理的异常, 代码不
我遇到了一个小问题,在调试了所有应用程序后,我注意到这是导致问题的文件,并向我返回了 UnhandledPromiseRejection 'use strict' const connection =
我对 Java(特别是 Android)相当陌生。我试图让用户从图库中选择图像,然后应用程序会将图像从图库复制到应用程序目录中的文件夹(以及显示他们在图像按钮中选择的图片)。但是,我收到“未处理的异常
我已从 iOS SDK 4.2 升级到 iOS SDK 5.0。当我现在尝试编译当前项目时,出现以下错误。 关于这里出了什么问题的任何想法?我已经阅读了所有我能找到的“mtouch failed wi
由于我正在考虑使用 WCF,所以我认为最好只是按照一个简单的教程来尝试一下。 3 小时后,我只有一个异常(exception)要显示。它不会消失。 我排除了没有加载 app.config 的可能性。如
我正在尝试在带有 .net 4.5 的 VS 2012 中使用带有 Telerik OpenAccess 的 WCF Plain 服务。 我尝试了 telerik 开发人员手册并创建了服务和客户端。
我遇到了这个错误 Program.exe 中 0x0049b946 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000090。 错误指向这一行: // thread
对于我的 Monodroid 应用程序,我想在出现未处理的异常后执行以下操作: 将错误发送到服务器。 通知用户应用程序已崩溃(可能使用 toast 消息)。 优雅地退出应用程序。 我已经实现了#1,但
我使用 JQuery 的表排序器对表进行排序,但我遇到了空表抛出异常的问题。所以我在脚本中添加了一个条件,但现在问题出在条件上。 : $(document).ready(function ($) {
我的构造函数的目标是: 打开一个文件读入特定字符串(“%%%%%”)之间存在的所有内容将每个读取行放在一个变量(历史记录)中将最终变量添加到 char 类型的双指针 (_stories)关闭文件。 但
我正在使用 Visual Studio 2005 和 IIS 6.0.. 我在事件日志中有这个警告。我试图找到它是什么。我工作时从未经历过这个异常(exception)。可以做什么,在哪里可以做,不再
我是一名优秀的程序员,十分优秀!