qt5.3 tableview最小尺寸不起作用

时间:2015-09-09 12:39:04

标签: qt qt5 qt5.4 qt5.3

我想使用qtableview创建日历小部件。我在Qt5.4上测试成功,但在Qt5.3上,qtableview最小尺寸不起作用。

我想设置固定尺寸300 * 300

Qt5.4很完美,但在Qt5.3中,tableview完全不能显示:http://i.stack.imgur.com/VzSjR.png

相同的代码,相同的固定大小(300 * 300),但Qt5.3无法完全显示。

您可以在here

下载我的演示代码

main.cpp中:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyModel *model = new MyModel;
    MyDelegate *delegate = new MyDelegate;
    MyTableView *view = new MyTableView(this);
    view->setModel(model);
    view->setItemDelegate(delegate);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(view);

    ui->widget->setLayout(layout);
    ui->widget->setStyleSheet("background-color:red;");
    ui->widget->setFixedSize(300, 300);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mydelegate.cpp:

MyDelegate::MyDelegate(QWidget *parent)
    : QItemDelegate(parent)
{

}

void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    painter->fillRect(option.rect, Qt::gray);
    painter->setPen(Qt::black);
    painter->drawText(option.rect, Qt::AlignCenter, index.data().toString());
    painter->restore();
}

mymodel.cpp:

MyModel::MyModel()
{

}

int MyModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return 6;
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return 7;
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::TextAlignmentRole)
        return Qt::AlignCenter;

    if (role == Qt::DisplayRole)
        return QString::number(index.row()) + " - " + QString::number(index.column());

    return QVariant();
}

mytableview.cpp:

MyTableView::MyTableView(QWidget *parent)
    : QTableView(parent)
{
    setTabKeyNavigation(false);
    setShowGrid(false);
    verticalHeader()->setVisible(false);
    horizontalHeader()->setVisible(true);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setSelectionBehavior(QAbstractItemView::SelectItems);
    setSelectionMode(SingleSelection);
    setFrameStyle(QFrame::NoFrame);
    horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    horizontalHeader()->setSectionsClickable(false);
    verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    verticalHeader()->setSectionsClickable(false);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

非常感谢!

0 个答案:

没有答案