[solved] QTableView Question

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
333onlyhalfevil
Junior Member
Posts: 24
Joined: Fri Apr 28, 2023 12:39 pm

[solved] QTableView Question

Post by 333onlyhalfevil » Sat Apr 13, 2024 6:12 am

I'm having some trouble getting a QTableView to work how I want it to in one of my scripts. I made one in the Qt designer in the *.ui file then edited it with the following code:

Code: Select all

var model = new QStandardItemModel(4,6);
var tableView = widgets1["testList"];
tableView.setModel(model);
tableView.verticalHeader().setVisible(false);
which all works as expected. However, I can't seem to get it past this. Everything I try to do after this causes the script to fail, typically with an error where it can't find something.

I tried editing the header with this code:

Code: Select all

var header = new QStringList("header1");
header.append("header2");
header.append("header3");
header.append("header4");
header.append("header5");
header.append("header6");
tableView.setHorizontalHeaderLabels(header);
with no luck.

I tried editing a single column header with this:

Code: Select all

tableView.setHeaderData(2, Qt.Horizontal, "test");
with no luck.

I tried editing the data within the table with this:

Code: Select all

for (var row = 0; row < 4; row++) {
        for (var col = 0; col < 2; col++) {
            var index = new QModelIndex(row, col);
            model.index(index);
            model.setData(index, 0);
        }
    }
with no luck.

Even something as simple as

Code: Select all

var test = new QString("test");
fails. It's weird that this line (and the others) wouldn't work as expected but the QStandardItemModel at the top works fine.

What do you think? What am I doing wrong? Thank you very much for your time.

333onlyhalfevil
Junior Member
Posts: 24
Joined: Fri Apr 28, 2023 12:39 pm

Re: QTableView Question

Post by 333onlyhalfevil » Sat Apr 13, 2024 4:48 pm

Nevermind. Figured it out. Thanks anyways. Needed to be doing it with QStandardItem and then setting it to the appropriate position within the QStandardItemModel. Note that each cell needs its own unique QStandardItem and multiple cells can't reference the same one.

User avatar
andrew
Site Admin
Posts: 9066
Joined: Fri Mar 30, 2007 6:07 am

Re: QTableView Question

Post by andrew » Mon Apr 15, 2024 9:37 am

For the sake of a complete answer for anyone reading this:

There are no QString or QStringList classes in the script environment.

Instead of

Code: Select all

var test = new QString("test");
use

Code: Select all

var test = "test";

Instead of

Code: Select all

var header = new QStringList("header1");
header.append("header2");
...
use

Code: Select all

var header = ["header1", "header2", ...];

CVH
Premier Member
Posts: 3491
Joined: Wed Sep 27, 2017 4:17 pm

Re: [solved] QTableView Question

Post by CVH » Mon Apr 15, 2024 2:36 pm

andrew wrote:
Mon Apr 15, 2024 9:37 am
use
var header = ["header1", "header2", ...];
Or use

Code: Select all

var header = [];
header.push("header1");
header.push("header2");
...

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”