代码之家  ›  专栏  ›  技术社区  ›  HiddenWindshield

在QT中单击菜单时出现SEGFULT

  •  1
  • HiddenWindshield  · 技术社区  · 7 年前

    我对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       
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Martin Höher    7 年前

    请看一下 QCoreApplication QApplication

    施工人员希望 argc 参数为a 对整数的引用

    在你的情况下,你通过 argc 论点 按价值 给您的 ProgName 班在内部,您将对该(本地)值的引用向下传递给的构造函数 QApplication 。问题是一旦构造函数调用完成,引用将无效/悬空。特别是在通话时 exec() 稍后在 run 方法时,应用程序对象将尝试访问失败并产生所观察到的崩溃的引用。

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

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