I don't see the difference between those two points...
The difference is that one adds new tabs, and the other adds new content to an existing layout. You talk a bit about adding content to existing layouts but then also talk about having to make custom widgets for each tab. It was worth clarifying whether you wanted one or the other because your code only demonstrates one.
I will need to have several tabs that each scroll on their own, but once that is established, all of them can receive more items to display. The entire QTabWidget, however, will not need to be viewed through a scrolling area.
OK, you want both.
Here is a single tab example:
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
layout
->addWidget
(new QLabel("A Label",
this));
layout
->addWidget
(new QLabel("B Label",
this));
setLayout(layout);
}
};
Q_OBJECT
public:
// The stuff inside the scroll area
layout
->setSizeConstraint
(QLayout::SetFixedSize);
// <<< this is the magic addAnother();
addAnother();
container->setLayout(layout);
scroll->setWidget(container);
// Put the scrolling area in to a tab page
addTab(scroll, "One");
// Cause a new one to be added every 2 seconds for demo purposes
connect(timer, SIGNAL(timeout()), SLOT(addAnother()));
timer->start(2000);
}
public slots:
void addAnother() {
layout->addWidget(new Widget(this));
}
};
int main(int argc, char *argv[])
{
TabWidget tw;
tw.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class Widget: public QFrame {
Q_OBJECT
public:
Widget(QWidget *p = 0): QFrame(p) {
setFrameStyle(QFrame::Panel | QFrame::Plain);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("A Label", this));
layout->addWidget(new QLabel("B Label", this));
setLayout(layout);
}
};
class TabWidget: public QTabWidget {
Q_OBJECT
QTimer *timer;
QVBoxLayout *layout;
public:
TabWidget(QWidget *p = 0): QTabWidget(p) {
// The stuff inside the scroll area
QWidget *container = new QWidget(this);
layout = new QVBoxLayout(container);
layout->setSizeConstraint(QLayout::SetFixedSize); // <<< this is the magic
addAnother();
addAnother();
container->setLayout(layout);
QScrollArea *scroll = new QScrollArea(this);
scroll->setWidget(container);
// Put the scrolling area in to a tab page
addTab(scroll, "One");
// Cause a new one to be added every 2 seconds for demo purposes
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(addAnother()));
timer->start(2000);
}
public slots:
void addAnother() {
layout->addWidget(new Widget(this));
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TabWidget tw;
tw.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
The magic is the sizeConstraint on the container's layout. This causes the layout to adopt the size of the contained widget's sizeHint()s rather than resize the contained widgets. For a variation on the theme look at the Extension Example
In general, if I create a QObject or derived class on the heap I always give it a parent thereby ensuring the memory is freed when the program exits (cleanly or by crashing, not that the latter ever happens to me ).
Bookmarks