I am writing an application in Qt with C++. In this application I have to display an amount of data in a text box.
Is there a way to use QTextEdit or QPlainTextEdit with the model/view concept in Qt? I only found list, tree, or table View classes with mvc functionality.
Is there a way to use QTextEdit or QPlainTextEdit with the model/view concept in Qt?
No.
For using model/view concept you need use already existed classes which inherit QAbstractItemView (such as: QColumnView, QHeaderView, QListView, QTableView and QTreeView) or inherit your custom class.
Have a look at the Qt documentation. There are the options you have:
http://doc.qt.io/qt-4.8/model-view-programming.html
Models
QAbstractItemModel provides an interface to data that is flexible
enough to handle views that represent data in the form of tables,
lists, and trees. However, when implementing new models for list and
table-like data structures, the QAbstractListModel and
QAbstractTableModel classes are better starting points because they
provide appropriate default implementations of common functions.
Views
QListView displays a list of items, QTableView displays data from
a model in a table, and QTreeView shows model items of data in a
hierarchical list. Each of these classes is based on the
QAbstractItemView abstract base class.
Controller
QAbstractItemDelegate is the abstract base class for delegates in
the model/view framework.
Related
I'm new to Qt and would like to know how to link data from a form I created to a table. I also created a QTableWidget. I understand I would need to implement some SQL code, but I am wondering if there is a simple method to map the data. Thank you for your help in advance.
Using the QTableVIew on top of a QAbstractTableModel is a good preferable solution if you want your application to be built with the MVC architecture. You need to create a new class which inherits the QAbstractTableModel. If so, there are some virtual functions such as setData(), data(), rowCount(), columnCount() and headerData() which you need to implement to populate the table with the database table.
I suggest you look into the following classes
QSqlDatabase
QSqlQuery
QAbstractTableModel
After implementing the class which inherits QAbstractTableMode, you could setup a TableView widget and set its model to display the contents.
QTreeView *view = new QTreeView(this);
view->setModel(tableModel);
If I understood the concept of the Qt models correct, then I can have multiple views, sharing the same model, so that when data is updated in the model, all views using it, will also update their view accordingly right?
Now I have multiple widgets in my application, which should have individual selections, but they should operate on the same underlying data. So when a row is added in one of the panels, the others should be able to display this new row as well.
Since the QListWidget provides all the features that I need, there would be no point in writing my own model and use it with a QListView. But I realized that QListWidget doesn't allow me to change the model, because the setModel()method is made private.
So is there some way that I can achieve this, without the need of writing a full model on my own? I'm rather new to Qt so maybe there is a ready made general purpose model, that I can use? But so far I haven't found one.
You are trying to use QListWidget to set your own model which is not possible.
You are better off using the MVC pattern the QT imposes. You can refer here on how to use the MVC pattern. This way would be more manageable and correct. Also you can make of the QStandardItemModel or the more general QAbstractListModel or QStringListModel model for lists. You can refer here for more details
But to answer your question yes they is a hack you can use.
Create a QListWidget and treat that widget as your model.
For other views create a QListView and set the model that is returned by the QListWidget
.
For e.g. refer
QAbstractItemModel* model = listWidget->model();
listView->setModel(model);
listView_2->setModel(model);
Then you can use the listWidget as your model. Any operation (add/delete) performed on the listWidget will also affect the listView and listView_2.
I am still of the opinion that instead of the hack creating your own model would be better and more correct.
How to use QTableView in Nokia Qt SDK (for mobiles). I referred some of documents but still I am not clearing about the QTableView. Please any one suggest how to use the QTableView.
I want to show the QTableView with three columns.
For the table data, you need to implement a model which will hold the data. If you don't require anything special, you can just subclass QAbstractTableModel.
Quoting the most important parts from the documentation:
When subclassing QAbstractTableModel,
you must implement rowCount(),
columnCount(), and data().
Editable models need to implement
setData(), and implement flags() to
return a value containing
Qt::ItemIsEditable.
You haven't specified where you get the data you are going to show in your table. That determines how you need to implement the required functions.
For even more simple model, use QStandardItemModel which already has a basic implementation for all required functions.
I want to use QCombobox as a the Combobox of Swing in Java. So i need to use Model for holding my object. How can i hold my object in QCombobox. (I think that I should hold data in Model because QCombobox was designed according to MVC Pattern ... )
Any help will be appreciated.
Depending on what you want to display with your QComboBox, you'll need to write your own model, inheriting QAbstractListModel, reimplementing rowCount()and data().
Then, use QComboBox::setModel() to make the QComboBox display it.
If you just want to display strings, you can use a QStringListModel, provided with Qt.
You can add a model to your QCombobox by using the setModel function. You can use a predefined model or create your own by inheriting from QAbstractItemModel.
Your model will contain your object to separate display from data.
Qt uses a simplified version of MVC which only has the Model / View parts.
You can use one of the provided subclasses of QAbstractItemModel if you don't need any specialized behaviour, which one to use depends on whether you keep your data in a file system or a data structure in memory.
You should read up the whole section on model/view programming in the Qt documentation.
I have a problem and I want to implement the MVC pattern to my QT application, that's why I need for example to inherite in my QTableWidget about another class like
myClass
{
myMethod();
}
but our QTableWidget is contained by our Mainwidows that is designed by the QT designer and generate an ui_MainWindow class !
Do you know how to do that ?
Is that a method inside the Qt Designer to do that ? or another method ?
Thank you for your answer !
You can use custom objects in QDesigner. Right click on the widget that you want to have as an instance of a something that is not available in designer and select "Promote to ...". In that dialog you can enter a custom class and a include file where the declaration for your class can be found.
BUT Qt implements uses MVC for a lot of the classes look at QTableView as opposed to QTableWidget, QTableView uses QAbstractItemModel as the model class and there is a pretty strict separation between the view and the model. See also An Introduction to Model/View Programming # Nokia