1

i want to create calendar widget use qtableview. i test successful on Qt5.4, but on Qt5.3, the qtableview minimum size not work.

i wanto to set fixed size 300 * 300

Qt5.4 is perfect,but in Qt5.3 tableview cant show completely: https://i.stack.imgur.com/VzSjR.png

same code, same fixed size (300 * 300), but Qt5.3 cant display completely.

you can download my Demo code at: 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);
}

thanks a lot !

sbw
  • 65
  • 8
  • Do you want/need to run it on QT 5.3? Why not use 5.4 and later? Maybe this is a QT 5.3 bug. Maybe the QT bug tracker has ticket for it https://bugreports.qt.io. For me to check it out, it love to have a complete example I can just copy, paste and run. – Ronny Brendel Sep 09 '15 at 13:27
  • yes, i need run this app on Qt5.3. i try to set fixed size to table cell, but still not work. – sbw Sep 10 '15 at 02:46
  • this example have many files, yuo can download all files: http://download.sbw.so/untitled1.tar.gz – sbw Sep 10 '15 at 02:48
  • I can't test on Qt 5.3, since I don't have it installed, sorry. I'd fiddle with the size/policy/fixedsize functions until it works (on all widgets that could cause this (table and parents?)). Good luck. – Ronny Brendel Sep 10 '15 at 19:47

0 Answers0