Dynamically adding items to QTreeView and QTableView Create the UI as follows, with the treeview on the left hand side and the table view on the right hand side. Notes The header file contains the models and the selection models for the tree view and the table view: QStandardItemModel * treeViewModel ; QItemSelectionModel * treeViewSelectionModel ; QItemSelectionModel * tableSelectionModel ; QStandardItemModel * tableModel ; In the .cpp file, the tree view models and table view models are set as the models for the selection models. treeViewSelectionModel -> setModel ( treeViewModel ) ; tableSelectionModel -> setModel ( tableModel ) ; The following code illustrates how to dynamically add a new row to the treeview. void MainWindow :: updateTreeView ( ) { QStandardItem * newRow = new QStandardItem ( QString ( "inserted row" ) ) ; QList < QStandardItem * > newRows ; newRows . append ( newRow ) ; aut...
C-style strings C style strings should be avoided except when interfacing with C libraries. C string library functions provide no bounds checking and memory allocation support. They are represented as an array of characters. Last character of the string is the null character \0 , so that code that uses the string knows where it ends. The space needed for a string is always one more than the number of readable characters. String Literals Strings written with quotes around them are string literals. They are stored in a read-only part of memory. Because they are stored in readonly sections, attempting to modify string literals is undefined behavior . Example: char * str = "world" ; str [ 0 ] = 'y' ; //undefined behavior If the code respected the standard and assigned the string literal to const char* , the compiler will catch attempts to modify string literals: const char * str = "world" ; str [ 0 ] = 'k' ; //compiler will f...
Friends Classes can declare other classes, other classes' member functions etc as friends and can access protected and private data members and methods. Example, given two classes A and B , you can say B is a friend of A as follows: class A { friend class B ; } Now all the methods of B can access the private and protected data members and methods of A . Dynamic memory allocation in objects If the amount of memory needed is unknown ahead of time, objects can dynamically allocate memory. Assume the CheckingAccount class from the previous article is updated as follows to include account number: checkingaccount.h : # ifndef CHECKING_H # define CHECKING_H # include <string> using std :: string ; class CheckingAccount { public : CheckingAccount ( std :: string accountNumber , double balance ) ; ~ Ch...
Comments
Post a Comment