0

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 <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()
{
    //...
}

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

Tom Regner
  • 6,856
  • 4
  • 32
  • 47
Scott
  • 35
  • 4
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Apr 19 '23 at 09:43
  • Firstly, use some spaces... QTabWidget*m_tabs; should be QTabWidget *m_tabs; and so on. Secondly, you delcare m_tabs but then use m_tab when creating the QTabWidget(). m_tab isn't declared anywhere and will cause heartache – Mike Apr 19 '23 at 10:08
  • Some more (minor) issues: `MainWindow`'s ctor should be `explicit`, Qt members should be created with `this` as `parent` parameter so Qt's own memory management can clean them up. Also, prefer to declare and define locals in one line, e.g. `QPlainTextEdit* e = new QPlainTextEdit(m_tabs);` – Friedrich Apr 19 '23 at 12:25
  • Also: "the application crashes without a reason" - I'd say a segfault is a pretty good reason. – Friedrich Apr 19 '23 at 12:28

2 Answers2

1

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.

Misinahaiya
  • 502
  • 2
  • 17
  • Note that an uninitialized pointer is not necessarily a `nullptr`. It could be garbage instead. Anyway, dereferencing it results in undefined behavior. Which is what you see as a segmentation fault. – Friedrich Apr 19 '23 at 12:19
0

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.

Mike
  • 419
  • 3
  • 7
  • 1
    Trying to assign to an undeclared variable would result in a compile time error, not a segmentation fault at runtime. This cannot possibly be what's causing OP's segfault (although maybe you've found a discrepency between the real code OP is using and what they posted here). – Jesper Juhl Apr 19 '23 at 10:59
  • @JesperJuhl Yes, agree. Just wasn't sure if m_tab was actually declared elsewhere, maybe incorrectly. But yes, I totally agree that would be picked up in Compilation. – Mike Apr 19 '23 at 11:12