在使用 Qt 和 QTabWidget 执行 cpp 代码时发生分段错误。

huangapple go评论66阅读模式
英文:

segment fault when executing a cpp code using Qt and QTabWidget

问题

我尝试编写一个使用C++语言的Qt代码。然而,当我到达QTabWidget部分时,应用程序崩溃,没有明显原因。打开终端,我看到类似以下内容:

在macOS上使用,我不知道是什么导致崩溃和“分段错误”。

这是我到目前为止尝试过的代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
class MainWindow:public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget*parent=nullptr);
private:
    void initialize_window();
    void initialize_tabs();
    void initialize_menus();
    QTabWidget*m_tabs;
    QMenuBar*m_menuBar;
    QMenu*m_fileMenu;
    //...
};

mainwindow.cpp

#include "mainwindow.h"
MainWindow::MainWindow(QWidget*parent)
    : QMainWindow(parent)
{
    initialize_window();
    initialize_tabs();
    initialize_menus();
}
void MainWindow::initialize_window()
{
    QPlainTextEdit*currentTextEditor = qobject_cast<QPlainTextEdit*>(m_tabs->currentWidget());
    setWindowTitle(currentTextEditor->toPlainText()); // 'test'
}
void MainWindow::initialize_tabs()
{
    m_tab = new QTabWidget();
    QPlainTextEdit*e;
    e = new QPlainTextEdit();
    e->setText("asdf");
    e->setReadOnly(true);
}
void MainWindow::initialize_menus()
{
    //...
}

我在macOS Ventura 13.3.1上使用arm64架构的mac。感谢任何解决方案或意见。谢谢

英文:

im trying to write a qt code using the c++ language. when i reach the QTabWidget part, however, the application crashes without a reason. opening the terminal and i see something like this:

zsh: segmentation fault  Easy-app-Example-16.app/Contents/MacOs/Easy-app-Example-16

using macos, i dunno what it's causing this reason of crashing and 'segmentation faulting'.

here's my code that ive tried so far:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include &lt;QMainWindow&gt;
#include &lt;QMenuBar&gt;
#include &lt;QMenu&gt;
#include &lt;QAction&gt;
class MainWindow:public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget*parent=nullptr);
private:
    void initialize_window();
    void initialize_tabs();
    void initialize_menus();
    QTabWidget*m_tabs;
    QMenuBar*m_menuBar;
    QMenu*m_fileMenu;
    //...
};

mainwindow.cpp

#include &quot;mainwindow.h&quot;
MainWindow::MainWindow(QWidget*parent)
    : QMainWindow(parent)
{
    initialize_window();
    initialize_tabs();
    initialize_menus();
}
void MainWindow::initialize_window()
{
    QPlainTextEdit*currentTextEditor = qobject_cast&lt;QPlainTextEdit*&gt;m_tabs-&gt;currentWidget();
    setWindowTitle(currentTextEditor-&gt;toPlainText()); // &#39;test&#39;
}
void MainWindow::initialize_tabs()
{
    m_tab = new QTabWidget();
    QPlainTextEdit*e;
    e = new QPlainTextEdit();
    e-&gt;setText(&quot;asdf&quot;);
    e-&gt;setReadOnly(true);
}
void MainWindow::initialize_menus()
{
    //...
}

im using macos ventura 13.3.1 on arm64 macs. any solution or comments are apprciated, thanks

答案1

得分: 1

Adjust your code.

// ...
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    initialize_tabs();
    initialize_window();
    initialize_menus();
}
// ...

其中一个导致分段错误的原因是使用了未声明/未分配的变量。从你的示例中可以看出,在初始化选项卡之前,您首先初始化了窗口。这导致程序首先启动窗口,然后发现选项卡窗口的QTabWidget::currentWidget()具有nullptr,因为它尚未初始化、分配或插入任何小部件。然后,由于nullptr属性实际上不能执行任何操作,所以zsh抛出了分段错误。

英文:

Adjust your code.

// ...
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    initialize_tabs();
    initialize_window();
    initialize_menus();
}
// ...

One of the cause of the segmentation error is because of using of undeclared/unallocated variables. From your example, you have first initialize the window before your tab is getting initialized. This results: the program first fires the windows up, but then it discovers that the tab widget has a nullptr with its QTabWidget::currentWidget() because of it's not initialized, allocated, and any widget-inserted. Then, because the nullptr attribute cannot actually do anything, so the zsh throws the segmentational error.

答案2

得分: 0

以下是翻译好的部分:

你已经声明了以下内容:

QTabWidget *m_tabs;

QMenuBar *m_menuBar;

QMenu *m_fileMenu;

但是随后尝试实例化以下内容:

m_tab = new QTabWidget();

从我所看到的地方,m_tab 并没有被声明过...

我认为你想要做的是这样的:

m_tabs = new QTabWidget();

另外,声明时大多数人会这样做:

QTabWidget *m_tabs;

QMenuBar *m_menuBar;

QMenu *m_fileMenu;

这样更易读。

英文:

You have declared the following:

QTabWidget*m_tabs;

QMenuBar*m_menuBar;

QMenu*m_fileMenu;

but then try to instantiate the following:

m_tab = new QTabWidget();

m_tab is not declared anywhere from what I can see...

I think you meant to do this:

m_tabs = new QTabWidget();

As a separate note, when declaring most people do the following:

QTabWidget *m_tabs;

QMenuBar *m_menuBar;

QMenu *m_fileMenu;

It's much more readable.

huangapple
  • 本文由 发表于 2023年4月19日 17:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定