I'm a total newbie, so don't be mean if this is total bogus. I want to write a pretty complex GUI, but I really don't want to write all my code into one single class. Does everything that's going to be in one window has to be in one class ? From what I already read I can create custom Widgets and add them into my code. But when I try, it doesn't work: The custom widget is always a seperate window. So I hope anybody can help me. I'm assuming I'm making a trivial mistake. I just can't find a tutorial for this since everybody just creates one custom widget and opens it from main as a new window. But I want to include my custom widget *into* the window.
I made a real simple version of my problem. Just two groupBoxes each having a button which are supposed to be on top of each other within ONE window. The top button represents everything I want to add from myWidget, the bottom one stands for all the other stuff (propably other custom widgets). The problem is they are always in different windows. How can I get them into one?
Please tell me why my thinking is wrong!
So here goes nothing:
#myWidget.cpp
#include "mywidget.h"
#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
{
layInMyW->addWidget(butInMyW);
grInMyW->setLayout(layInMyW);
grInMyW->setVisible(true);
}
#include "mywidget.h"
#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
myWidget::myWidget(QWidget *parent) : QWidget(parent)
{
QGroupBox *grInMyW = new QGroupBox("Group In myWidget");
QHBoxLayout *layInMyW = new QHBoxLayout();
QPushButton *butInMyW = new QPushButton("Button in myWidget");
layInMyW->addWidget(butInMyW);
grInMyW->setLayout(layInMyW);
grInMyW->setVisible(true);
}
To copy to clipboard, switch view to plain text mode
#main.cpp
#include "mywidget.h"
#include <QApplication>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
myWidget *myW = new myWidget;
layout->addWidget(myW);
layout->addWidget(butInMain);
gr->setLayout(layout);
gr->show();
return a.exec();
}
#include "mywidget.h"
#include <QApplication>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGroupBox *gr = new QGroupBox();
QVBoxLayout *layout = new QVBoxLayout();
myWidget *myW = new myWidget;
QPushButton* butInMain = new QPushButton("Button in Main");
layout->addWidget(myW);
layout->addWidget(butInMain);
gr->setLayout(layout);
gr->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
I don't know if I even need the GroupBoxes, but I read that for a custom widget i have to include some Layout, for its size to be clear. If there is an easier way, I'd really appreciate you telling me !
But I'd be thankful for any answer I can get !!
Bookmarks