Navigation

    Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Search
    • Unsolved
    1. Home
    2. Tags
    3. qsqlquerymodel
    Log in to post

    • UNSOLVED Segmentation Fault after assigning QSqlQueryModel from function return in switch statement
      General and Desktop • c++ sql qsqlquery qsqlquerymodel sigsegv • • achoy011  

      14
      0
      Votes
      14
      Posts
      159
      Views

      @RyanSolanki Please mark as solved if you are happy the issue is resolved. Also consider reading up further about Qt's memory management model (maybe it would make sense to pass a parent during the QSqlQueryModel's construction to avoid all that manual memory management or else perhaps smart pointers if there is no clear parent to assign ownership to). Furthermore, SGaist's point about passing a pointer to a function whose signature expects a const reference cannot be understated. If the function expects a pointer, give it a pointer. If it expects a const reference, give it an object created on the stack! https://doc.qt.io/qt-5/qsqlquerymodel.html#details The documentation is good; use it! It is unclear to me why you are using functions such as "clear" for the model. The setQuery() function already clears the model. Why mess around with new and delete several times when you can just pass in a new query to modify the existing model? Futhermore, why bother clear()-ing the model immediately before you delete it? The destructor is called during the delete, which itself frees up any resources, per the documentation. There are deeper problems of comprehension going on here!
    • SOLVED QSqlQueryModel with escaped characters in database
      General and Desktop • qsqlquerymodel setquery • • rjmx  

      4
      0
      Votes
      4
      Posts
      89
      Views

      @Christian-Ehrlicher Yep. QIdentityProxyModel works fine. Thanks.
    • UNSOLVED fill combobox with query results, but also <select>
      General and Desktop • qcombobox qsqlquerymodel • • hobbyProgrammer  

      2
      0
      Votes
      2
      Posts
      221
      Views

      @hobbyProgrammer You cannot have a combo which is both bound to a model and has some extra row inserted into it --- it's one or the other. There are several possible approaches: Make your SQL query return a UNION ALL which includes a literal <select> as an extra row. Not really nice, because it makes the <select> be an extra row from the query. Copy the result set from the query into another model (you probably only need a list, perhaps a QStringList) where you insert the extra <select> as an item and use that as the combo's model. If you don't get a better example, this is the most obvious to code. Interpose a QIdentityProxyModel between the query model and the combo, and add the extra row there. I suspect this is the way it's supposed to be done(?).
    • SOLVED What to subclass?
      General and Desktop • model-view qsqlquerymodel subclassing editable model • • Pl45m4  

      9
      0
      Votes
      9
      Posts
      462
      Views

      @christian-ehrlicher said in What to subclass?: Especially compared to the idea to parse the data somehow from a sqlite file Where I have said that? :) I never had the idea to "parse" the sqlite file directly. I will try to get the data with a query first and see what I can do with it :) (I guess QSqlResult is the key?!)
    • SOLVED QTableView does not show data when populating with QSqlQueryModel / stored procedure
      General and Desktop • qtableview qsqlquerymodel sql server stored procedur • • donnpie  

      3
      0
      Votes
      3
      Posts
      642
      Views

      So I tried the suggestions by @jwernerny. I dumped the QSqlQueryModel and went with a QStandardItemModel. TableView now displays correctly. Code now looks like this: void MainWindow::on_btnLoadTable_clicked() { conn = new Connection("LOCALHOST\\SQLEXPRESS", "Plants"); stdModel = new QStandardItemModel(this); conn->init(); //Set driver and connection options conn->open(); qry1 = new QSqlQuery(conn->db); const int COL_COUNT = 3; if (qry1->exec("spBatchesPerPlant")) { qDebug() << "Executing query..."; int row = 0; //used for counting rows in the query while (qry1->next()) //Loop through the results { for (int col = 0; col < COL_COUNT; col++) { stdItem = new QStandardItem(qry1->value(col).toString()); stdModel->setItem(row, col, stdItem); } row++; } ui->tableView->setModel(stdModel); } } Motto of the story: stored procedures generate forward only queries. QSqlQueryModel does not work for forward only queries, so use a standard item (or custom) model.
    • SOLVED How to show QSqlQuryModel in QML?
      QML and Qt Quick • qml sql tableview qsqlquerymodel • • noone  

      3
      0
      Votes
      3
      Posts
      500
      Views

      Hi, Add a setter to your model that is Q_INVOKABLE and pass your query through it. Don't forget to add proper error checking to give feedback to your user if something goes wrong.
    • SOLVED MySQL Stored Procedure, accessing a single result set
      General and Desktop • sql mysql qsqlquery qsqlquerymodel • • DaveK 0  

      3
      0
      Votes
      3
      Posts
      1328
      Views

      Hi, There’s also no need to allocate the query on the heap as the QSqlQueryModel::setQuery method parameter suggests.
    • UNSOLVED Threading and Showing db tables with extra info
      General and Desktop • qtableview thread qsqlquerymodel qtablemodel • • Sanchezy  

      2
      0
      Votes
      2
      Posts
      669
      Views

      Hi and welcome to devnet, You can use a proxy model that adds the columns you want/need. You can keep updating the DB content and trigger the GUI update at some known interval or when a certains amount of changed happened to the database.
    • UNSOLVED Best practices to use CRUD SQL in QML
      QML and Qt Quick • qml sql qsqlquery qsqlquerymodel • • Cleiton Bueno  

      6
      0
      Votes
      6
      Posts
      3222
      Views

      @Cleiton-Bueno Yes. If the query string is correct it will work.
    • UNSOLVED Dodanie kolumny z checkboxem do QTableView
      Polish • qtableview qsqlquerymodel checkbox • • drock  

      2
      0
      Votes
      2
      Posts
      1194
      Views

      Można użyć modelu proxy, który będzie interpretował jakieś dane (np. "0" i "1") jako checkbox, np. class MyProxy : public QIdentityProxyModel { public: MyProxy(QObject* parent) : QIdentityProxyModel(parent) {} QVariant data(const QModelIndex& index, int role) const override { if (role == Qt::CheckStateRole && index.column() == 0) { QString str_value = QIdentityProxyModel::data(index, Qt::DisplayRole).toString(); return (str_value == "1") ? Qt::Checked : Qt::Unchecked; } return QIdentityProxyModel::data(index, role); } bool setData(const QModelIndex& index, const QVariant& value, int role) override { if (role == Qt::CheckStateRole && index.column() == 0) { QString str_value = (value.toInt() == Qt::Checked) ? "1" : "0"; return QIdentityProxyModel::setData(index, str_value, Qt::EditRole); } else return QIdentityProxyModel::setData(index, value, role); } Qt::ItemFlags flags(const QModelIndex& index) const override { Qt::ItemFlags f = QIdentityProxyModel::flags(index); if (index.column() == 0) f |= Qt::ItemIsUserCheckable; return f; } }; oczywiście numer kolumny i dane rozpoznawane jako "zaznaczony" można sobie dostosować. Takiego modelu można użyć potem tak: QSqlTableModel* model = new QSqlTableModel(parent, database); MyProxy* proxy = new MyProxy(parent); proxy->setSourceModel(model); tableView->setModel(proxy);
    • UNSOLVED QSqlQueryModel downcasting problem !
      General and Desktop • tableview qsqlquerymodel downcasting • • _compiler  

      4
      0
      Votes
      4
      Posts
      1038
      Views

      @_compiler Model *derived = static_cast<QSqlQueryModel*>(model); This isn't a valid cast because you can't implicitly cast to a derived type (see @mrjj's comment). Model *derived = static_cast<Model *>(model); Is valid but unsafe, as @SGaist pointed out. Ultimately, what is not working?
    • Using QComboBox with QSqlQueryModel
      General and Desktop • c++ qt4 qcombobox model-view item qsqlquerymodel data • • Aleksey_K  

      2
      0
      Votes
      2
      Posts
      1922
      Views

      Yeah, according to QComboBox code should work: QVariant QComboBox::itemData(int index, int role) const { Q_D(const QComboBox); QModelIndex mi = d->model->index(index, d->modelColumn, d->root); return d->model->data(mi, role); } Will implement this.
    • QSqlQueryModel shows empty table with correct headers.
      General and Desktop • sql qt5.4 qsqlquery qsqlquerymodel odbc • • Charlie42  

      1
      0
      Votes
      1
      Posts
      754
      Views

      No one has replied

    • [SOLVED] How to store sql's ID and access it on QListView on click event to get this id?
      General and Desktop • qlistview qsqlquerymodel • • mkolenda  

      2
      0
      Votes
      2
      Posts
      987
      Views

      I would do: int id = model->index(index.row(), 1).data().toInt(); The sequence of the columns in the query is the same as in the model and the view so 0=name, 1=id. The model is obtained either directly from the class where you have it or from the view using QListView::model().
    • [SOLVED]function returning QSQLQueryModel
      General and Desktop • function qsqlquerymodel • • cpuin  

      6
      0
      Votes
      6
      Posts
      2134
      Views

      You have more information about why you can't copy QObject derived class here
    • [SOLVED] QListView key-value pair from SQL table
      General and Desktop • qlistview qsqlquerymodel qtsql • • mkolenda  

      2
      0
      Votes
      2
      Posts
      1408
      Views

      Hi and welcome to devnet, They index provides you the row/column of the clicked item, so you can request the data from the index of the id column and the same row that you just clicked. Hope it helps
    • QtableView how to find the proper signal
      General and Desktop • qtableview signal selection qsqlquerymodel • • cpuin  

      12
      0
      Votes
      12
      Posts
      6176
      Views

      Sorry for the late reply, the page says that there's nothing to be found
    • [Solved]Error while using 'bindValue' for my QSqlQuery
      General and Desktop • qsqlquery qsqlquerymodel bindvalue • • ealione  

      3
      0
      Votes
      3
      Posts
      1175
      Views

      Thanks for the heads up. I had no idea. Since I tried to use a model and failed, I became curious. I have done one more example using this tutorial, and it went fine. Do you have any idea why is it complaining about my database not being open, when it clearly is, judging from the fact that my query worked fine.