c++ - 在QT中单击菜单时出现段错误
问题描述
我对 QT 开发很陌生,我正在尝试用菜单创建我的第一个窗口。问题是,每当我在尝试调试时实际单击“文件”菜单时都会出现分段错误。
这是 QT 5.10,在 Fedora Linux 64 位上运行
我的头文件有:
private:
QApplication app;
Ui::MainWindow ui;
QMainWindow mainWindow;
public:
explicit ProgName(int argc, char *argv[], QObject *parent = nullptr);
int run();
...其余的只是为了简洁而省略的标准 QT 样板。我的主要源文件:
#include "progname.h"
int main(int argc, char *argv[])
{
ProgName pn(argc, argv, nullptr);
return pn.run();
}
ProgName::ProgName(int argc, char *argv[], QObject *parent) :
QObject(parent),
app(argc, argv)
{
ui.setupUi(&mainWindow);
}
int ProgName::run()
{
mainWindow.show();
return app.exec();
}
真正长的一个,UI 文件,用 QT creator 制作:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QTreeView" name="mainTreeView"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="mainMenuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>28</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
</widget>
<addaction name="menuFile"/>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
有任何想法吗?
编辑:这是堆栈跟踪:
1 __strlen_avx2 0x7ffff5832c37
2 QCoreApplication::arguments() 0x7ffff6a7a63b
3 argv0BaseName() 0x7fffe88d0101
4 QXcbIntegration::wmClass() const 0x7fffe88d05fd
5 QXcbWindow::create() 0x7fffe88e596b
6 QXcbIntegration::createPlatformWindow(QWindow *) const 0x7fffe88d153e
7 QWindowPrivate::create(bool, unsigned long long) 0x7ffff6fd32fe
8 QWidgetPrivate::create_sys(unsigned long long, bool, bool) 0x7ffff7714ced
9 QWidget::create(unsigned long long, bool, bool) 0x7ffff77153ad
10 QMenuPrivate::adjustMenuScreen(QPoint const&) 0x7ffff785bf68
11 QMenu::popup(QPoint const&, QAction *) 0x7ffff785f801
12 QMenuBarPrivate::popupAction(QAction *, bool) 0x7ffff786c402
13 QMenuBarPrivate::setCurrentAction(QAction *, bool, bool) 0x7ffff786e508
14 QMenuBar::mousePressEvent(QMouseEvent *) 0x7ffff786ee72
15 QWidget::event(QEvent *) 0x7ffff7722baf
16 QMenuBar::event(QEvent *) 0x7ffff787014b
17 QApplicationPrivate::notify_helper(QObject *, QEvent *) 0x7ffff76e392c
18 QApplication::notify(QObject *, QEvent *) 0x7ffff76eb6cf
19 QCoreApplication::notifyInternal2(QObject *, QEvent *) 0x7ffff6a76be7
20 QApplicationPrivate::sendMouseEvent(QWidget *, QMouseEvent *, QWidget *, QWidget *, QWidget * *, QPointer<QWidget>&, bool) 0x7ffff76ea6a2
21 QWidgetWindow::handleMouseEvent(QMouseEvent *) 0x7ffff773d47b
22 QWidgetWindow::event(QEvent *) 0x7ffff773fb1f
23 QApplicationPrivate::notify_helper(QObject *, QEvent *) 0x7ffff76e392c
24 QApplication::notify(QObject *, QEvent *) 0x7ffff76eb174
25 QCoreApplication::notifyInternal2(QObject *, QEvent *) 0x7ffff6a76be7
26 QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *) 0x7ffff6fc98a3
27 QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *) 0x7ffff6fcb495
28 QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff6fa479b
29 userEventSourceDispatch(_GSource *, int ( *)(void *), void *) 0x7fffe892cb60
30 g_main_context_dispatch 0x7ffff2195b77
31 g_main_context_iterate.isra 0x7ffff2195f20
32 g_main_context_iteration 0x7ffff2195fac
33 QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff6ac7c2f
34 QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff6a7596a
35 QCoreApplication::exec() 0x7ffff6a7e094
36 ProgName::run progname.cpp 21 0x401aaf
37 main progname.cpp 7 0x401961
解决方案
请查看QCoreApplication和QApplication的构造函数。
构造函数期望argc
参数是对整数的引用。
在您的情况下,您按值argc
将参数传递给您的类的构造函数。在内部,您将对此(本地)值的引用传递给. 问题是,一旦构造函数调用完成,引用就会无效/悬空。特别是,在稍后调用该方法时,应用程序对象将尝试访问失败并产生观察到的崩溃的引用。ProgName
QApplication
exec()
run
长话短说:作为一种解决方法,只需通过argc
参数作为参考:
ProgName::ProgName(int &argc, char *argv[], QObject *parent) :
QObject(parent),
app(argc, argv)
{
ui.setupUi(&mainWindow);
}
推荐阅读
- python-3.x - 如何用变量中的其他值替换列表中的值?
- regex - 如何使正则表达式考虑负数?
- php - Symfony 4 一个实体的几种形式(步骤)
- ios - Firebase .onCall Cloud 函数未从 Swift Xcode 项目中调用
- matlab - 用向量给出的参数在一个图中绘制函数(颜色问题)
- javascript - Redux 容器单元测试 - 无法访问包装组件的道具
- android - 并行运行多个虚拟Android系统
- fortran - 开放语句中“iostat”参数的含义是什么?
- wordpress - 如何使用 Wordpress 修剪单词功能
- wordpress - 如何在 docker-compose 中的 wordpress 和 wp-cli 之间共享卷