简体   繁体   English

将选项卡添加到QTabWidget后,Qt App崩溃

[英]Qt App Crashing after adding a tab to QTabWidget

The title more or less says it all. 标题差不多说明了一切。 I create a widget, add it to QTabWidget and when I switch to the new tab the app crashes. 我创建了一个小部件,将其添加到QTabWidget,当我切换到新选项卡时,应用程序崩溃。

preferencestab.h (The widget I want to add) preferencestab.h(我要添加的小部件)

#ifndef PREFERENCESTAB_H
#define PREFERENCESTAB_H

#include <QWidget>

#include "tab.h"

class PreferencesTab : public QWidget
{
    Q_OBJECT
public:
    explicit PreferencesTab(QWidget *parent = 0);
    ~PreferencesTab();
    int num;

private:


private slots:
};

#endif // PREFERENCESTAB_H

preferencestab.cpp preferencestab.cpp

#include "preferencestab.h"

#include <QDebug>

PreferencesTab::PreferencesTab(QWidget *parent) : QWidget(parent)
{
}

PreferencesTab::~PreferencesTab()
{

}

tabmanager.cpp (subclass of QTabWidget and where I add the new tab) tabmanager.cpp(QTabWidget的子类以及在其中添加新标签的位置)

...
void TabManager::openPreferences()
{
    PreferencesTab *pref = new PreferencesTab();
    int index = this->addTab(pref, "Edit");

    this->setCurrentIndex(index); // It crashes on this line
}

If I remove the line where it crashed it succeeds at creating the new tab but crashes when I manually switch to it. 如果删除崩溃的行,则可以成功创建新选项卡,但是当我手动切换到该选项卡时,崩溃。

It must be something stupid but I just can't find the error. 这一定是愚蠢的,但我找不到错误。 Help Please 请帮助

Try to debug your application. 尝试调试您的应用程序。

In QtCreator, you start the debug mode by pressing F5 (⌘-Y on macs) instead of Ctrl-R. 在QtCreator中,您可以通过按F5(在Mac上为⌘-Y)而不是Ctrl-R来启动调试模式。 This will launch a debugger (eg GDB) which tells QtCreator (which then tells you) what kind of error occurred. 这将启动调试器(例如GDB),该调试器将告诉QtCreator(然后告诉您)发生了哪种类型的错误。 You also can set breakpoints, step through your code and inspect the variable values (before the error occures of course). 您还可以设置断点,单步执行代码并检查变量值(当然,在发生错误之前)。

Another option is to print out the variables using qDebug. 另一个选择是使用qDebug打印出变量。 Candidates are the this pointer, pref and the index variable; 候选对象是this指针, prefindex变量; check their values. 检查它们的值。

That said, the problem which causes the crash is most likely outside the PreferencesTab class, maybe even outside the TabManager class. 就是说,导致崩溃的问题很可能在PreferencesTab类之外,甚至在TabManager类之外。 That the code within a method of a class is executed doesn't mean that is was a "valid" call (= the this pointer was valid). 执行类方法中的代码并不意味着这是一个“有效”调用(= this指针有效)。 So checking the this pointer is always a good idea to track calls to null or invalid pointers. 因此,检查this指针始终是跟踪对空指针或无效指针的调用的好主意。

If TabManager is a QObject , even debugging the this pointer will fail (if it is invalid), because qDebug() << this will ask for the metaObject() , which will be (most likely) an invalid read if the this pointer is invalid. 如果TabManagerQObject ,则即使调试this指针也将失败(如果它无效),因为qDebug() << this将要求metaObject() ,如果this指针为(则很可能是)无效读取无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM