Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Widget inside another widget in QT?

Tags:

c++

qt4

hi how to add widget inside widget

i created main widget, and for the main widget headerbar come from another widget. here the code below

main.cpp

#include <QApplication>
#include "mainwindow.h"

int main(int argl,char *argv[])
{
    QApplication test(argl,argv);

    mainWindow *window=new mainWindow();
    window->setWindowState(Qt::WindowFullScreen);
    window->show();

    return test.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include <QtGui>
#include "headerbar.h"
#include <QGridLayout>

mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{

    QGridLayout *layout;
    headerBar *Header=new headerBar(this);
    layout->addWidget(Header,0,0);
    this->setLayout(layout);
}


mainWindow::~mainWindow()
{

}

headerbar.cpp

#include "headerbar.h"

headerBar::headerBar(QWidget *parent) : QWidget(parent)
{
    this->setMaximumHeight(24);
}

headerBar::~headerBar()
{

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

class mainWindow : public QWidget
{
    Q_OBJECT
public:
    mainWindow(QWidget *parent = 0);
    ~mainWindow();

signals:

public slots:

};

#endif // MAINWINDOW_H

headerbar.h

#ifndef HEADERBAR_H
#define HEADERBAR_H

#include <QWidget>

class headerBar : public QWidget
{
    Q_OBJECT
public:
    headerBar(QWidget *parent = 0);
    ~headerBar();

signals:

public slots:

};

#endif // HEADERBAR_H

while compile this code no errors. but when i am trying to run it's through error "exited with code -1073741819"

please help me to fix this issue

like image 400
saravanan Avatar asked Sep 13 '10 11:09

saravanan


1 Answers

While you use layout, you have never created and assigned an instance to it:

QGridLayout *layout; // no initialization here
headerBar *Header = new headerBar(this);
layout->addWidget(Header,0,0); // layout is uninitialized and probably garbage

You should create it first before using it:

QGridLayout *layout = new QGridLayout(this);
like image 175
Georg Fritzsche Avatar answered Nov 15 '22 19:11

Georg Fritzsche