简体   繁体   中英

Can QTreeView be added to QDockWidget

Does anyone has a short example (hopefully in C++) to illustrate adding a QTreeView to a QDockWidget?

I tried to use QLayout as an intermediate object, as:

QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();

QTreeView w;
w.setModel(&model);
w.setWindowTitle(QObject::tr("Simple Tree Model"));

QGridLayout     *layout;
layout = new QGridLayout;
layout->addWidget(&w, 0, 0, 1, 3);
swatch1->setLayout(layout);

Where swatch1 is of a type from QDockWidget. my code runs, but the tree does not appear on the DockWidget.

I saw QTreeWidget examples, but since the tree view will be changed frequently, I prefer to use a QTreeView.

Does anyone can provide a short example on how to put QTreeView onto QDockWidget? Thanks

Jay, I want to add a bit here. Your first suggested code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QDockWidget* dock = new QDockWidget;
    setCentralWidget(dock);

    QTreeView* treeView = new QTreeView;
    dock->setWidget(treeView);

    QFileSystemModel* treeModel = new QFileSystemModel;
    treeModel->setRootPath(QDir::currentPath());
    treeView->setModel(treeModel);
}

I tried something similar. But, during compiling, dock->setWidget(treeView) failed and has an error message saying this is not allowed. I could not understand since QTreeView is a widget, why this is not allowed. Alternatively, for example, I tried dock->setWidget(treeWidget) for a QTreeWidget item passed compilation.

The problem solved. This is my original code:

Qt Code: Switch view
QTreeView w;
w.setModel(&model);
swatch1->setWidget(&w);
w.setEnabled(true);

The code is in the scope of an object method. The QTreeView instance created on the stack will be destroyed as soon as the method exits, and it will be removed from the dock widget. That is why the code runs, and I cannot see the tree.

Thibaut B. and Jay, thank you both very much. Your postings confirms to me that the code I wrote by itself is correct, but something else is wrong ...

You can use QDockWidget::setWidget to set the contents of the dock window. For example

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QDockWidget* dock = new QDockWidget;
    setCentralWidget(dock);

    QTreeView* treeView = new QTreeView;
    dock->setWidget(treeView);

    QFileSystemModel* treeModel = new QFileSystemModel;
    treeModel->setRootPath(QDir::currentPath());
    treeView->setModel(treeModel);
}

Alternatively, you could use something like this if you wanted to have the tree within a layout in the dock:

QWidget* container = new QWidget;
dock->setWidget(container);

QLayout* layout = new QHBoxLayout;
container->setLayout(layout);

QTreeView* treeView = new QTreeView;
layout->addWidget(treeView)

QDockWidget is a QWidget, so if swatch1 is your DockWidget:

QMainWindow mw;
QTreeView *w = new QTreeView(&mw);
QDockWidget *swatch1 = new QDockWidget("Simple Tree Model", &mw);
swatch1->setWidget(w);
// addDockWidget is a method of QMainWindow
mw.addDockWidget(Qt::RightDockWidgetArea, swatch1);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM