S
S
sddvxd2018-08-19 15:20:02
Qt
sddvxd, 2018-08-19 15:20:02

Why in the QTabWidget widget does not show the list of strings in the QListView widget?

Good afternoon!
There was such a problem: QListView items are not displayed if it is in a QTabWidget widget

//Заголовочные файлы


#ifndef TABUDOBR_H
#define TABUDOBR_H

#include <QtWidgets>

class TabUdobr : public QWidget
{
    Q_OBJECT
private:
    QListView* pListView;
public:
    explicit TabUdobr(QWidget *parent = nullptr);
signals:

public slots:
};

#endif // TABUDOBR_H

#ifndef TABCOOKING_H
#define TABCOOKING_H

#include <QtWidgets>

class TabCooking : public QWidget
{
    Q_OBJECT
private:
    QLabel* plabel;
public:
    explicit TabCooking(QWidget *parent = nullptr);

signals:

public slots:
};

#endif // TABCOOKING_H

And source code:
TabUdobr::TabUdobr(QWidget *parent) : QWidget(parent)
{
    pListView = new QListView;
    QVBoxLayout* playout = new QVBoxLayout;
    QStringListModel model;
    model.setStringList(QStringList() << "Item1" << "Item2" << "Item3");
    QItemSelectionModel itemselect(&model);
    pListView->setModel(&model);
    pListView->setSelectionModel(&itemselect);
    playout->addWidget(pListView);
    setLayout(playout);
}
//main.cpp
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget mainWindow;

    QTabWidget* pbar = new QTabWidget;

    QVBoxLayout* pvboxlay = new QVBoxLayout;
    pbar->addTab(new TabUdobr, "TestTab");
    pvboxlay->addWidget(pbar);
    pvboxlay->addWidget(new TabUdobr);
    mainWindow.setLayout(pvboxlay);
    mainWindow.show();

    return a.exec();
}

In the code above, I create a widget that represents a link. It contains a QListView with three values.
If I collect like this, then everything is displayed:
//main.cpp
    QListView* pListView = new QListView;
    QVBoxLayout* playout = new QVBoxLayout;
    QStringListModel model;
    model.setStringList(QStringList() << "Item1" << "Item2" << "Item3");
    QItemSelectionModel itemselect(&model);
    pListView->setModel(&model);
    pListView->setSelectionModel(&itemselect);
    playout->addWidget(pListView);
    mainWindow.setLayout(playout);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sddvxd, 2018-08-19
@sddvxd

Blunted specifically ... When you exit the class constructor, all members that are not members of the class are deleted ... Declared members of the class and assigned pointers - everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question