在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();
}

而真正长的,是用QT创建器制作的UI文件:

<?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的构造函数。问题是,一旦构造函数调用完成,引用将无效/悬而未决。特别是,稍后在run方法中调用exec()时,应用程序对象将尝试访问失败并产生观察到的崩溃的引用。

长话短说:作为一种解决方法,只需通过argc参数作为参考:

ProgName::ProgName(int &argc, char *argv[], QObject *parent) :
QObject(parent),
app(argc, argv)
{
ui.setupUi(&mainWindow);
}

相关内容

  • 没有找到相关文章

最新更新