I will describe my issue.
I have a class for tab, which looks approximately like this:
class Tab : public QWidget
{
public:
Tab();
...
private:
...
QGraphicsView *view;
...
};
In Tab()
I create the view
. Now in my main window I would like to have a QTabWidget
, which contains some tabs, where each tab displays a QGraphicsView
. It looks approximately like this:
class MainWindow : QMainWindow
{
public:
MainWindow();
...
private:
...
QTabWidget *tabWidget;
...
};
and
MainWindow::MainWindow()
{
tabWidget = new QTabWidget(this);
tabWidget->addTab(new Tab, tr("tab 1"));
tabWidget->addTab(new Tab, tr("tab 2"));
setCentralWidget(tabWidget);
}
This doesn't display the QGraphicsView
s in the tabs. To make the QGraphicsView
s visible, I would have to write something like this:
MainWindow::MainWindow()
{
tabWidget = new QTabWidget(this);
tab1 = new Tab;
tab2 = new Tab;
tabWidget->addTab(tab1->view, tr("tab 1"));
tabWidget->addTab(tab2->view, tr("tab 2"));
setCentralWidget(tabWidget);
}
and view
would have to be public in the class Tab
, which really looks bad. The class Tab
has some private functions, which are used to construct view
and I don't want view
to be public in Tab
.
How to get around this?
I was thinking of creating a class, which inherits from QGraphicsView
and adding my own functions, but I'm not quite sure how to do this so that it works for me.
Thank you for your answers and comments.
It's most likely because Tab
doesn't have a layout (or you even have not added view
as a child of Tab
widget). You didn't show Tab
constructor, so I can't know for sure.
If a tab can contain only a view, then you need to derive Tab
class from QGraphicsView
instead of QWidget
. Additional view
class member should be removed, and you can use Tab
objects as if it would be QGraphicsView
objects.
If a tab can contain other widgets, then you should add a layout to your Tab
object and add the view and other children to this layout. Example:
Tab::Tab() {
QVBoxLayout* main_layout = new QVBoxLayout(this);
view = new QGraphicsView();
main_layout->addWidget(view);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.