Skip to content

Instantly share code, notes, and snippets.

@zenlinux

zenlinux/hbsplitter.cpp

Last active Jul 2, 2019
Embed
What would you like to do?
Qt5 Gui program compiles but generates segmentation fault.
/* file : hbsplitter.cpp */
#include <QHBoxLayout>
#include <QSplitter>
#include <QString>
#include "hbsplitter.h"
#include "mlabelwidget.h"
#include "mtextedit.h"
Hbsplitter::Hbsplitter(QWidget *parent) : QWidget(parent)
{
QHBoxLayout *hbox = new QHBoxLayout(this);
QSplitter *splitter1 = new QSplitter(Qt::Horizontal);
Mtextedit *myMtextedit = new Mtextedit();
Mlabelwidget *myMlabelwidget = new Mlabelwidget();
QString newLabel;
newLabel = "Qt with cpp.";
myMlabelwidget->show();
myMlabelwidget->changeText(newLabel);
/* if I uncomment the above line I get "zsh: segmentation fault ./stage3"
* where stage3 is the name of the executable */
splitter1->addWidget(myMtextedit);
splitter1->addWidget(myMlabelwidget);
hbox->addWidget(splitter1);
}
/* file : hbsplitter.h */
#pragma once
#include <QWidget>
class Hbsplitter : public QWidget
{
public:
Hbsplitter(QWidget *parent = 0);
};
/* file : main.cpp */
#include <QApplication>
#include "hbsplitter.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setStyleSheet("QSplitter::handle { background-color: #333;}");
Hbsplitter window;
window.resize(700, 300);
window.setWindowTitle("Notes to SVG");
window.show();
return app.exec();
}
/* file : mlabelwidget.cpp */
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <iostream>
#include "mlabelwidget.h"
Mlabelwidget::Mlabelwidget(QWidget *parent) : QWidget(parent)
{
QHBoxLayout *hbox = new QHBoxLayout(this);
QLabel *label = new QLabel();
label->setText("This is Qt");
hbox->addWidget(label);
}
void Mlabelwidget::changeText(QString input) {
std::cout << "Mlabelwidget::changeText() called." << "\n";
/* std::cout << input << "\n"; */
label->setText(input);
std::cout << "Mlabelwidget::changeText() completed." << "\n";
}
/* file : mlabelwidget.h */
#pragma once
#include <QWidget>
#include <QLabel>
class Mlabelwidget : public QWidget
{
public:
Mlabelwidget(QWidget *parent = 0);
QLabel *label;
void changeText(QString input);
};
/* file : mtextedit.cpp */
#include <QWidget>
#include <QHBoxLayout>
#include <QTextEdit>
#include "mtextedit.h"
Mtextedit::Mtextedit(QWidget *parent) : QWidget(parent)
{
QHBoxLayout *hbox = new QHBoxLayout(this);
QTextEdit *mtextedit = new QTextEdit();
hbox->addWidget(mtextedit);
}
/* file : mtextedit.h */
#pragma once
#include <QWidget>
class Mtextedit : public QWidget
{
public:
Mtextedit(QWidget *parent = 0);
};
######################################################################
# Automatically generated by qmake (3.0) Tue Jul 2 04:42:55 2019
######################################################################
TEMPLATE = app
TARGET = stage3
INCLUDEPATH += .
# Input
HEADERS += hbsplitter.h mlabelwidget.h mtextedit.h
SOURCES += hbsplitter.cpp main.cpp mlabelwidget.cpp mtextedit.cpp
QT += widgets svg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment