Important: Please read the Qt Code of Conduct -

Creating headers with comboboxes according to treeview.

  • Hi,
    I have a qtreeview structure that I created with the data I got from the database. Since the data of this database changes instantaneously, the number of columns and rows of the treeview also changes. I want to add a custom combobox to the column headers of this treeview I created. I searched a lot but couldn't find anything solid. Could you help.

  • Lifetime Qt Champion

    @Nevez said in Creating headers with comboboxes according to treeview.:

    I searched a lot but couldn't find anything solid.

    What does this mean? Did you already tried the forum search? The 4th result is

  • @Christian-Ehrlicher
    I already tried this code. But the Boxes list gives an error.
    I tried several methods but I couldn't get past this error. Could you please give it a try? How can I overcome this problem.

  • Lifetime Qt Champion

    @Nevez said in Creating headers with comboboxes according to treeview.:

    But the Boxes list gives an error.

    And how should we help you with this statement?

  • @Christian-Ehrlicher
    ASSERT failure in QList<Combobox*>::operator[]: "index out of range",

    this is the error i got.

  • Lifetime Qt Champion

    @Nevez Then use a debugger to see from where the crash is coming and post the relevant code. I really wonder why nobody knows how to properly use a debugger here. It's a basic tool for programming...

  • This post is deleted!

  • @Christian-Ehrlicher
    you are right, nobody is very experienced in that subject, including me. But luckily I was able to fix the problem.
    But now i am having a problem in another part of the code. I am using some data from database in QVariant MainWindow::data function. Every time the function is used, I create a Sql Model and run a Sql query to access the data in the database. As you know, this function is called many times. This will be a heavy burden for me in the future. How can I do this with a single query.

    QVariant MainWindow::data(const QModelIndex &item, int role) const{

    QString kod,unvan;
    double alacak,borc,bakiye;
    QSqlQueryModel model;
    model.setQuery("SELECT * FROM cariler order by kod");
    kod = model.record(item.row()).value("kod").toString();
    unvan = model.record(item.row()).value("unvan").toString();
    alacak = model.record(item.row()).value("alacak").toDouble();
    borc = model.record(item.row()).value("borc").toDouble();
    bakiye = alacak - borc;
    switch (role) {
    case Qt::DisplayRole:
        switch (item.column())
        case 0:
             return kod;
        case 1:
           return unvan;
        case 2:
            return alacak;
        case 3:
             return borc;
        case 4:
             return bakiye;
    case Qt::BackgroundRole:
        if(bakiye<0 && item.column() ==4)
              return QBrush(Qt::red);
              return QBrush(Qt::white);
        return QSqlQueryModel::data(item,role);

    //define bakiye QSqlQueryModel::data(index(item.row(),3)).toDouble() - QSqlQueryModel::data(index(item.row(),4)).toDouble() // not Working*****

  • @Nevez
    You do not to be creating a model or running a query in your data() method. As per all the doc examples of models, SQL or otherwise, create your model and fill it with data outside of the data() method at an appropriate place.

    If you are using a QSql... model or query you do not need to need to do any data fetching for DisplayRole in data() method: the QSql... model/query does that, you can just call the base method if you override data() for e.g. BackgroundRole role.

    TBH, I'm not really sure what you're doing given that you have MainWindow::data() method. It has the signature of QSqlQueryModel::data(), which is something you would override, but surely MainWindow is some kind of QMainWindow and has nothing to do with models, SQL or otherwise? If you need to change the behaviour of QSqlQueryModel::data() you must derive your own class from QSqlQueryModel for your model and override methods in that.

  • @JonB
    my goal is to just take the data from my database and create a model and then set that model to a treeview in main.cpp. And now everything is working as I want.
    Example: in my "main.cpp"
    MyModel mymodel;
    QTreeview *tree = new QTreeView();

    The Screen Output is like this ;
    ![alt text](66f51b59-2b38-4fb8-8520-b9539f4616a7-photo_2022-01-12_09-21-51.jpg image url)

    • But as I said before; The query always runs in my Data function to get this output. Please explain this to me.
      How can I define this query elsewhere only once and use it without needing to define it in my Data function? (with or without override)

    NOT: I can send you my project file if you want. It just consists of 2 headers and 2 cpps and a main.cpp file.

Log in to reply