I am new to stack overflow, I am using c++, I need to connect with my sqlite database which i am successfully accessing, Now I want to display that information (contents of tables from sqlite) into a table either Qtablewidget or Qtable view. I am novice to Qt. How should I do this?
How can I display information in QTablewidget in c++?
Related
I am writing a program in Qt creator. I have a treeview and a tableview. Each time an item in the treeview is clicked, I need to update the tableview. The problem is that the tables realted to each tree item is different. Since the tableview works with SQLite databases, I have decided to create a database and a qsqltablemodel for each of the tree items and set the qsqltablemodel for the tableview upon each click on the tree item. I guess the tableview should update then. To implement this, I need at least 60 databases and models and am thinking if there is any issue with having mutiple databases and how to manage them effectively.
I'm working with Oracle APEX 19 and using the Web Source Module to get data from https://swapi.co/api/people/. However, I haven't been able to find a way to map/get the data displayed on datatypes that are lists, like the films and starships properties.
I maps the other properties (eg.: name, height, mass, etc.) fine and I get that a list wouldn't be a valid datattype for a column, I was thinking more in the lines of it becoming a new table or a way to connect different object to do a master detail screen.
I'm rather new with the tool and I haven't been able to find a way or a tutorial to teach me how to do it using APEX. Is it at all possible through only APEX? Should I try to do it with PL/SQL instead?
Thx in advance
I can't figure out why my QTableView won't display any data. I've searched all other questions about this problem and it seems the problem is usually someone trying to create the model on the stack and letting it go out of scope...
I am creating the model on the heap so this is not the problem, yet I still get no data in the View. The column headers from my sql table are shown correctly. What could be wrong with this code?
// db is my database wrapper and database returns a reference to the database
QSqlTableModel *tradeHistoryModel = new QSqlTableModel(this, db->database());
// table_tradeHistory is my QTableView created elsewhere
table_tradeHistory->setModel(tradeHistoryModel);
tradeHistoryModel->setTable("mytrades");
tradeHistoryModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
if (!tradeHistoryModel->select())
{
// err is just a handy macro for displaying fancy error output
// the call to select does NOT return false so this never gets called
err("model select failed!");
}
// debug is another macro, the output shows 200 rows (which is correct)
debug(QString("Got %1 rows..").arg(tradeHistoryModel->rowCount()));
So there is no error shown because select() returns true, and the debug output shows the model selected all 200 rows, but still no data appears in the View...
Thanks for any help!
Edit: This must have been a Qt or Qt Designer bug... I went back into designer and "morphed" the QTableView into a QTreeView, then switched it back and now all of a sudden it is showing the data... I did not change any of the code when I did this... wtf? If it is a bug I'm not sure I can reproduce it again...
The problem might be the tradeHistoryModel->select(). I don't
think it is selected properly. Try to call it separately, not on a IF statement. Also check the compatibility of the data, the data on the db and that on the QTableView. I had a similar problem with QDate and I managed to solve it after modifying the format it was displayed on the QTableView. The problem was PostgreSQL and Qt uses different date format.
To be sure it has nothing to do with this problem try
model->setFilter(filter);
model->select();
or
modelsql->removeColumn(0);
to remove a column that might contain this problem. Good luck!
Thanks for the post. The same thing happened to me, and morphing the QTableView into a QTreeView and then back to a QTableView also fixed it for me. When I compared before and after, the original bad ui had the section <attribute name="verticalHeaderDefaultSectionSize"><number>0</number></attribute>. Maybe that made the header expand to the entire space of the view, and with no space to show rows of data, Qt had no reason to request data from the model.
I started with a QTableWidget but later switched to a QTableView to implement batching through canFetchMore/fetchMore, so maybe this problem is something left over from the QTableWidget.
I'm thinking of porting my access application to Qt. I am interested to learn how to do continouos subforms, sub custom widgets for presenting/editing/inserting data from recordset in a verically scrollable non datagrid fashion. Meaning I could put button, label, combo, lineEdit... whatever, for every record.
I like QTableView and delegates. I just don't know if it could be modified to fully emulate access subform.
Sidequestion(maybe the same answer)... how do they DO those continuous forms in access under the hood.
thanks
... not real application data in that example recordset
Qt MVC is probably the best/easiest answer for your question ( http://qt-project.org/doc/qt-4.8/model-view-programming.html ), and with QTableView you should be able to achieve what you want.
Another solution could be: if you have a fix set of column items in every row you could simply design a QWidget with the contents of the row and paste your items (rows) into a QVerticalLayout.
Although I would sugget to try with MVC because that's the preferred way and in this case you could even port it to use QML UI if you want (while you can use the same data classes for the 'backend'). QML is definitely the best approach for (even slightly) animated UIs, and it's mature enough to use it already (it's part of Qt 4.8 and will be the 'star' of Qt 5).
I still remember in Delphi, developer can just make the UI(textbox, listbox...) directly connect to database, and then when user click a button, just call the post action, then the data will be saved automatically.
What I want to know is that is there any similar mechanism in MFC? Or I can use GetDlgItem(...).Text and then use this value to save to database ?
Or any other suggestions will be appreciated.
In VC++ , you have to use Microsoft ActiveX Data Object Library (ADO typelib) .
To store data you can follow these steps:
1.Retrive data from all controls
2.Validate the data retrived
3.Use sql query to store the data to database.
You can use ODBC API which is independent of any database management system.
http://msdn.microsoft.com/en-us/library/ms714562(VS.85).aspx
http://www.odbc.net/api/index.shtml
To be fair on Delphi these are specialized widgets, not the ordinary GDI textbox etc, but controls with an additional database aware layer that are connected to dataset and tables.
.NET has something similar concepts too, don't know about MFC
MFC's abstraction of data in Doc/View/Frame is in CDocument. When you save the document, MFC prompts the user for the file name if the file does not have a saved path, then construct a CArchive on the file and triggers CDocument::Serialize. You can store the connection string in your document class and use it to save data in CDocument::Serialize.
If you have a file based database, it is easier to integrate. Override CDocument::OnNewDocument to create a new file based database for the document, and
override CDocument::OnOpenDocument to read from existing database. If you don't have a file based database, you can suppress the file dialog with a CDocument::SaveModified override that saves the data and clears the modified flag.