QTable that is a QSplitter child does not show vertical scrollbar



  • I have the following ui, the mainwindow has a horizontal splitter with two children, mDiffTable and mPreviewTable. When I move the splitter to make the mPreviewTable not fit in the ui a vertical scrollbar should appear, But it does not.

    @ mCentralwidget = new QWidget(FusionExplorer);
    mCentralwidget->setObjectName(QString::fromUtf8("mCentralwidget"));
    verticalLayout = new QVBoxLayout(mCentralwidget);
    verticalLayout->setContentsMargins(2, 2, 2, 2);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    mVerticalSplitter = new QSplitter(mCentralwidget);
    mVerticalSplitter->setObjectName(QString::fromUtf8("mVerticalSplitter"));
    QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(mVerticalSplitter->sizePolicy().hasHeightForWidth());
    mVerticalSplitter->setSizePolicy(sizePolicy);
    mVerticalSplitter->setOrientation(Qt::Horizontal);
    mVerticalSplitter->setHandleWidth(3);
    mExplorer = new QTreeView(mVerticalSplitter);
    mExplorer->setObjectName(QString::fromUtf8("mExplorer"));
    QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy1.setHorizontalStretch(0);
    sizePolicy1.setVerticalStretch(0);
    sizePolicy1.setHeightForWidth(mExplorer->sizePolicy().hasHeightForWidth());
    mExplorer->setSizePolicy(sizePolicy1);
    mExplorer->setUniformRowHeights(true);
    mVerticalSplitter->addWidget(mExplorer);
    mHorizontalSplitter = new QSplitter(mVerticalSplitter);
    mHorizontalSplitter->setObjectName(QString::fromUtf8("mHorizontalSplitter"));
    QSizePolicy sizePolicy2(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
    sizePolicy2.setHorizontalStretch(35);
    sizePolicy2.setVerticalStretch(0);
    sizePolicy2.setHeightForWidth(mHorizontalSplitter->sizePolicy().hasHeightForWidth());
    mHorizontalSplitter->setSizePolicy(sizePolicy2);
    mHorizontalSplitter->setOrientation(Qt::Vertical);
    mHorizontalSplitter->setHandleWidth(3);
    mDiffTable = new QTableView(mHorizontalSplitter);
    mDiffTable->setObjectName(QString::fromUtf8("mDiffTable"));
    QSizePolicy sizePolicy3(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy3.setHorizontalStretch(0);
    sizePolicy3.setVerticalStretch(35);
    sizePolicy3.setHeightForWidth(mDiffTable->sizePolicy().hasHeightForWidth());
    mDiffTable->setSizePolicy(sizePolicy3);
    mDiffTable->setSelectionMode(QAbstractItemView::SingleSelection);
    mDiffTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mDiffTable->setTextElideMode(Qt::ElideNone);
    mDiffTable->setShowGrid(true);
    mHorizontalSplitter->addWidget(mDiffTable);
    mDiffTable->horizontalHeader()->setDefaultSectionSize(335);
    mDiffTable->horizontalHeader()->setMinimumSectionSize(30);
    mDiffTable->horizontalHeader()->setStretchLastSection(true);
    mPreviewTable = new QTableView(mHorizontalSplitter);
    mPreviewTable->setObjectName(QString::fromUtf8("mPreviewTable"));
    QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy4.setHorizontalStretch(0);
    sizePolicy4.setVerticalStretch(0);
    sizePolicy4.setHeightForWidth(mPreviewTable->sizePolicy().hasHeightForWidth());
    mPreviewTable->setSizePolicy(sizePolicy4);
    mPreviewTable->setStyleSheet(QString::fromUtf8("QAbstractItemView::item QImage {\n"
    " border: 1px solid black\n"
    "}"));
    mPreviewTable->setSelectionMode(QAbstractItemView::NoSelection);
    mPreviewTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mPreviewTable->setTextElideMode(Qt::ElideNone);
    mPreviewTable->setShowGrid(true);
    mHorizontalSplitter->addWidget(mPreviewTable);
    mPreviewTable->horizontalHeader()->setDefaultSectionSize(335);
    mPreviewTable->horizontalHeader()->setMinimumSectionSize(30);
    mPreviewTable->horizontalHeader()->setStretchLastSection(true);
    mPreviewTable->verticalHeader()->setVisible(false);
    mVerticalSplitter->addWidget(mHorizontalSplitter);

        verticalLayout->addWidget(mVerticalSplitter);
    

    @



  • I created a complete example based on your code. When I move the splitter down, so that all of mPreviewTable does not fit, then a vertical scrollbar shows up for me. Does the example below reproduce the problem you are having? If not, can you modify it so that it does? That will make it easier to pinpoint what the problem is.

    @#include <QtGui>

    int main(int argc, char** argv)
    {
    QApplication app(argc, argv);
    QMainWindow window;
    QWidget *mCentralwidget = new QWidget(&window);
    window.setCentralWidget(mCentralwidget);

    QVBoxLayout *verticalLayout = new QVBoxLayout(mCentralwidget);
    verticalLayout->setContentsMargins(2, 2, 2, 2);

    QSplitter *mVerticalSplitter = new QSplitter(mCentralwidget);

    QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(mVerticalSplitter->sizePolicy().hasHeightForWidth());

    mVerticalSplitter->setSizePolicy(sizePolicy);
    mVerticalSplitter->setOrientation(Qt::Horizontal);
    mVerticalSplitter->setHandleWidth(3);

    QTreeView *mExplorer = new QTreeView(mVerticalSplitter);

    QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy1.setHorizontalStretch(0);
    sizePolicy1.setVerticalStretch(0);
    sizePolicy1.setHeightForWidth(mExplorer->sizePolicy().hasHeightForWidth());

    mExplorer->setSizePolicy(sizePolicy1);
    mExplorer->setUniformRowHeights(true);
    mVerticalSplitter->addWidget(mExplorer);

    QSplitter *mHorizontalSplitter = new QSplitter(mVerticalSplitter);
    QSizePolicy sizePolicy2(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
    sizePolicy2.setHorizontalStretch(35);
    sizePolicy2.setVerticalStretch(0);
    sizePolicy2.setHeightForWidth(mHorizontalSplitter->sizePolicy().hasHeightForWidth());

    mHorizontalSplitter->setSizePolicy(sizePolicy2);
    mHorizontalSplitter->setOrientation(Qt::Vertical);
    mHorizontalSplitter->setHandleWidth(3);

    QTableView *mDiffTable = new QTableView(mHorizontalSplitter);

    QSizePolicy sizePolicy3(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy3.setHorizontalStretch(0);
    sizePolicy3.setVerticalStretch(35);
    sizePolicy3.setHeightForWidth(mDiffTable->sizePolicy().hasHeightForWidth());

    mDiffTable->setSizePolicy(sizePolicy3);
    mDiffTable->setSelectionMode(QAbstractItemView::SingleSelection);
    mDiffTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mDiffTable->setTextElideMode(Qt::ElideNone);
    mDiffTable->setShowGrid(true);

    mHorizontalSplitter->addWidget(mDiffTable);
    mDiffTable->horizontalHeader()->setDefaultSectionSize(335);
    mDiffTable->horizontalHeader()->setMinimumSectionSize(30);
    mDiffTable->horizontalHeader()->setStretchLastSection(true);

    QTableView *mPreviewTable = new QTableView(mHorizontalSplitter);
    QStandardItemModel *model = new QStandardItemModel(4, 8);
    for (int row = 0; row < 4; ++row) {
    for (int column = 0; column < 8; ++column) {
    QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    model->setItem(row, column, item);
    }
    }
    mPreviewTable->setModel(model);

    QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy4.setHorizontalStretch(0);
    sizePolicy4.setVerticalStretch(0);
    sizePolicy4.setHeightForWidth(mPreviewTable->sizePolicy().hasHeightForWidth());
    mPreviewTable->setSizePolicy(sizePolicy4);
    mPreviewTable->setStyleSheet(QString::fromUtf8("QAbstractItemView::item QImage {\n"
    " border: 1px solid black\n"
    "}"));
    mPreviewTable->setSelectionMode(QAbstractItemView::NoSelection);
    mPreviewTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mPreviewTable->setTextElideMode(Qt::ElideNone);
    mPreviewTable->setShowGrid(true);

    mHorizontalSplitter->addWidget(mPreviewTable);
    mPreviewTable->horizontalHeader()->setDefaultSectionSize(335);
    mPreviewTable->horizontalHeader()->setMinimumSectionSize(30);
    mPreviewTable->horizontalHeader()->setStretchLastSection(true);
    mPreviewTable->verticalHeader()->setVisible(false);
    mVerticalSplitter->addWidget(mHorizontalSplitter);

    verticalLayout->addWidget(mVerticalSplitter);

    window.show();
    return app.exec();

    }@



  • It is reproducible in the following code.

    @#include <QtGui>

    class PreviewModel : public QAbstractTableModel
    {
    Q_OBJECT

    public:
    PreviewModel() : QAbstractTableModel() {}
    ~PreviewModel(void) {}

        // Table Model Implementations.
        virtual int rowCount(const QModelIndex &parent) const
        {
           return 1;
        }
        virtual int columnCount(const QModelIndex &parent) const
        {
            return 2;
        }
        virtual QVariant data(const QModelIndex &index, int role) const
        {
                if (!index.isValid()) {
                        return QVariant();
                }
    
                switch(role) {
                        case Qt::DecorationRole:
                                {
                                    return QVariant(QImage("c:/portrait.jpg"));
                                }
                        default:
                                return QVariant();
                }
        }
        virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
        {
                if (role != Qt::DisplayRole) {
                        return QVariant();
                }
                if (orientation == Qt::Vertical) {
                        return QVariant();
                }
    
                if (section == 0) {
                        return QVariant(QString("LHS Preview"));
                }
                else if (section == 1) {
                        return QVariant(QString("RHS Preview"));
                }
    
                return QVariant();
        }
    

    };

    int main(int argc, char** argv)
    {
    QApplication app(argc, argv);
    QMainWindow window;
    QWidget *mCentralwidget = new QWidget(&window);
    window.setCentralWidget(mCentralwidget);

    QVBoxLayout *verticalLayout = new QVBoxLayout(mCentralwidget);
    verticalLayout->setContentsMargins(2, 2, 2, 2);

    QSplitter *mVerticalSplitter = new QSplitter(mCentralwidget);

    QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(mVerticalSplitter->sizePolicy().hasHeightForWidth());

    mVerticalSplitter->setSizePolicy(sizePolicy);
    mVerticalSplitter->setOrientation(Qt::Horizontal);
    mVerticalSplitter->setHandleWidth(3);

    QTreeView *mExplorer = new QTreeView(mVerticalSplitter);

    QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy1.setHorizontalStretch(0);
    sizePolicy1.setVerticalStretch(0);
    sizePolicy1.setHeightForWidth(mExplorer->sizePolicy().hasHeightForWidth());

    mExplorer->setSizePolicy(sizePolicy1);
    mExplorer->setUniformRowHeights(true);
    mVerticalSplitter->addWidget(mExplorer);

    QSplitter *mHorizontalSplitter = new QSplitter(mVerticalSplitter);
    QSizePolicy sizePolicy2(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
    sizePolicy2.setHorizontalStretch(35);
    sizePolicy2.setVerticalStretch(0);
    sizePolicy2.setHeightForWidth(mHorizontalSplitter->sizePolicy().hasHeightForWidth());

    mHorizontalSplitter->setSizePolicy(sizePolicy2);
    mHorizontalSplitter->setOrientation(Qt::Vertical);
    mHorizontalSplitter->setHandleWidth(3);

    QTableView *mDiffTable = new QTableView(mHorizontalSplitter);

    QSizePolicy sizePolicy3(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy3.setHorizontalStretch(0);
    sizePolicy3.setVerticalStretch(35);
    sizePolicy3.setHeightForWidth(mDiffTable->sizePolicy().hasHeightForWidth());

    mDiffTable->setSizePolicy(sizePolicy3);
    mDiffTable->setSelectionMode(QAbstractItemView::SingleSelection);
    mDiffTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mDiffTable->setTextElideMode(Qt::ElideNone);
    mDiffTable->setShowGrid(true);

    mHorizontalSplitter->addWidget(mDiffTable);
    mDiffTable->horizontalHeader()->setDefaultSectionSize(335);
    mDiffTable->horizontalHeader()->setMinimumSectionSize(30);
    mDiffTable->horizontalHeader()->setStretchLastSection(true);

    QTableView *mPreviewTable = new QTableView(mHorizontalSplitter);
    // QStandardItemModel *model = new QStandardItemModel(4, 8);
    // for (int row = 0; row < 4; ++row) {
    // for (int column = 0; column < 8; ++column) {
    // QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    // model->setItem(row, column, item);
    // }
    // }
    // mPreviewTable->setModel(model);

    QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy4.setHorizontalStretch(0);
    sizePolicy4.setVerticalStretch(0);
    sizePolicy4.setHeightForWidth(mPreviewTable->sizePolicy().hasHeightForWidth());
    mPreviewTable->setSizePolicy(sizePolicy4);
    mPreviewTable->setStyleSheet(QString::fromUtf8("QAbstractItemView::item QImage {\n"
    " border: 1px solid black\n"
    "}"));
    mPreviewTable->setSelectionMode(QAbstractItemView::NoSelection);
    mPreviewTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mPreviewTable->setTextElideMode(Qt::ElideNone);
    mPreviewTable->setShowGrid(true);

    mHorizontalSplitter->addWidget(mPreviewTable);
    mPreviewTable->horizontalHeader()->setDefaultSectionSize(335);
    mPreviewTable->horizontalHeader()->setMinimumSectionSize(30);
    mPreviewTable->horizontalHeader()->setStretchLastSection(true);
    mPreviewTable->verticalHeader()->setVisible(false);
    mVerticalSplitter->addWidget(mHorizontalSplitter);

    verticalLayout->addWidget(mVerticalSplitter);

    window.show();
    mPreviewTable->setModel(new PreviewModel());
    mPreviewTable->resizeRowsToContents();
    return app.exec();

    }
    @



  • The problem here is that you only have 1 row and that the default "scrollMode":http://doc.qt.nokia.com/4.7/qabstractitemview.html#ScrollMode-enum is set to QAbstractItemView::ScrollPerItem by default. Therefore, when there is only one item it decides it does not need a scrollbar. Try setting it to

    @mPreviewTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);@

    instead. That gives a vertical scrollbar for me when running your example.



  • Thanks a lot, it works now :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.