ayanda83
5th November 2017, 17:28
Hi ladies and gents, I have a QTableView with 4 columns and I would like to permanently resize the second column to 50% the entire width of the QTableView. I thought this would be something easy to figure out but it has proven otherwise. I have attached a screenshot of what I am trying to achieve below. As can be seen in the screenshot below, the second column is 3-times longer than the first and the third column.12655
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableView->setColumnWidth(1, 900);
model = new QStandardItemModel(74, 4, ui->innertabWidget);
this->setWindowTitle("Tender Bot");
QFile sitesFile("websites.txt");
if(!sitesFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Something went wrong, file did not open" <<endl;
}
else
{
QTextStream out(&sitesFile);
int count = 0;
while(!out.atEnd())
{
QModelIndex index = model->index(count, 0, QModelIndex());
model->setData(index, sitesFile.readLine(0).simplified());
count++;
}
}
model->setHeaderData(0, Qt::Horizontal, "Websites");
model->setHeaderData(1, Qt::Horizontal, "Downloaded");
model->setHeaderData(2, Qt::Horizontal, "Date");
model->setHeaderData(3, Qt::Horizontal, "Buttons");
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableView->setModel(model);
}I thought line 6 in the code snippet above would achieve my desired outcome but it doesn't work.
Please assist.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableView->setColumnWidth(1, 900);
model = new QStandardItemModel(74, 4, ui->innertabWidget);
this->setWindowTitle("Tender Bot");
QFile sitesFile("websites.txt");
if(!sitesFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Something went wrong, file did not open" <<endl;
}
else
{
QTextStream out(&sitesFile);
int count = 0;
while(!out.atEnd())
{
QModelIndex index = model->index(count, 0, QModelIndex());
model->setData(index, sitesFile.readLine(0).simplified());
count++;
}
}
model->setHeaderData(0, Qt::Horizontal, "Websites");
model->setHeaderData(1, Qt::Horizontal, "Downloaded");
model->setHeaderData(2, Qt::Horizontal, "Date");
model->setHeaderData(3, Qt::Horizontal, "Buttons");
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableView->setModel(model);
}I thought line 6 in the code snippet above would achieve my desired outcome but it doesn't work.
Please assist.