user489152
user489152

Reputation: 907

adding QGraphicsView to QBoxLayout

I am a QT newbie and trying to play around with Apps. I have just coded a very trivial App with some buttons. The main idea is to have a small "logo" in my App. LAter I would like to add some background image as well.

I have coded from an example App with a grid layout within which is a QBoxLayout which groups my buttons.

As you can see in my code, I have tried adding the Logo everywhere. When I added it in main.cpp, I have two views one showing buttons and the other my logo. Of course I do not want this. So I tried adding it in mainwindow.cpp but in this case, I don't see my Logo appearing anywhere at all :(

Please advise.

Here is the code:

main.cpp:

#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);  

    Window window;
    window.show();
/*
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QGraphicsPixmapItem item(QPixmap("/home/marc/Desktop/Niranjana/Images/logo.9.png"));
    scene.addItem(&item);
    view.show();
*/

    return a.exec();
}

mainwindow.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
#include <QRadioButton>


class QGroupBox;

class Window : public QWidget
{
    Q_OBJECT

public:
    Window(QWidget *parent = 0);
    void onCheck_remote(int flag);
    void onCheck_local(int flag);


private:

    QRadioButton *button_local;
    QRadioButton *button_remote;
    QGroupBox *createPushButtonGroup();


};

#endif

mainwindow.cpp

#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"

Window::Window(QWidget *parent)
    : QWidget(parent)
{
    QGridLayout *grid = new QGridLayout;

    QGraphicsScene scene;
    QGraphicsPixmapItem item(QPixmap("/home/test/logo.png"));
    QGraphicsView view(&scene);
    scene.addItem(&item);
    view.show();

    grid->addWidget(view.viewport(), 1, 1);


    grid->addWidget(createPushButtonGroup(), 2, 1);

    setLayout(grid);
    setWindowTitle(tr("My App"));
    resize(480, 420);
}

QGroupBox *Window::createPushButtonGroup()
{
    QGroupBox *groupBox = new QGroupBox();

  /*
    QGraphicsScene scene;
    QGraphicsPixmapItem item(QPixmap("/home/marc/Desktop/Niranjana/Images/logo.9.png"));
    QGraphicsView view(&scene);
    scene.addItem(&item);
    scene.setBackgroundBrush(Qt::white);
    view.show();
*/
    QPushButton *button1 = new QPushButton(tr("&Start"));
    QPushButton *button2 = new QPushButton(tr("&Stop"));

    button_local = new QRadioButton(tr("&with power"));
    button_remote = new QRadioButton(tr("without power"));
    button_local->setChecked(1);


    QVBoxLayout *vbox = new QVBoxLayout;
   // vbox->addSpacing(10);
   // vbox->addWidget(view.viewport());
    //vbox->addSpacing(10);
    vbox->addWidget(button1);
    vbox->addSpacing(10);
    vbox->addWidget(button2);
    vbox->addSpacing(50);
    vbox->addWidget(button_local);
    vbox->addWidget(button_remote);
    vbox->addStretch(1);
    groupBox->setLayout(vbox);


    return groupBox;
}

Upvotes: 2

Views: 1792

Answers (1)

Chris Browet
Chris Browet

Reputation: 4276

You should

  • Make your scene, view, item pointer members of your window class
  • Instantiate them in your Window ctor
  • do grid->addWidget(view, 1, 1);

Otherwise, all the items instantiated on the stack in the ctor will get deleted when exiting.

Upvotes: 2

Related Questions