Posts

Showing posts from January, 2020

QTreeView programmatic selection

Image
TreeView programmatic selection Create the UI as follows, drop  QTreeView , a couple of  QLineEdit s, and a search  QPushButton . The UI can look something like the following, with the TreeView on top: Implement the code in  mainwindow.h  as follows: # ifndef MAINWINDOW_H # define MAINWINDOW_H # include <QMainWindow> # include <QStandardItemModel> # include <QItemSelectionModel> # include <QMap> # include <QModelIndex> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow ; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public : MainWindow ( QWidget * parent = nullptr ) ; ~ MainWindow ( ) ; private : Ui :: MainWindow * ui ; QStandardItemModel model ; QItemSelectionModel * selectionModel ; void init ( ) ; QMap < QString , QModelIndex > searchItemToIndexMap ; private slots : void onSelectionChanged ( const QItemSelection & selected , co

C++ Tour Part II

Image
Pointers and Dynamic Memory Dynamic memory is useful when you don't know the memory requirements at compile time. Stack and the Heap Memory is divided into the  stack  and the  heap  or  free store . Stack memory is automatically managed, whereas heap memory is not.  Activation Records  are blocks of memory on the stack that are used to implement functions. For example, when  main  calls  foo ,  foo 's activation record is at the top of the stack. Once  foo  returns, the activation record for  foo  is marked as deallocated, and another function call can make use of that space. Any parameters passed from  main  to  foo  are copied into  foo 's activation record. Below is an illustration for the activation record from Wikipedia: Memory allocated on the heap lives indepedently of the function calls. Once has to make sure to deallocate memory allocated on the heap. Some languages such as C# and Python have garbage collection (GC) for this purpose but C++ does not.

C++ Tour Part I

C++ Tour Hello World Here is the hello world program for C++: # include <iostream> int main ( ) { std :: cout << "Hello, World" << std :: endl ; return 0 ; } Building a C++ Program Building a C++ program is a three step process: Code is run through  preprocessor  which processes meta information The code is  compiled  where each .cpp file is translated to .o object file Individual object files are  linked  together into a single application. Main function Main function is where program execution starts. It comes in two flavors: with and without arguments. With arguments, it looks as follows: int main ( int argc , char * argv [ ] ) where  argc  gives the number of commandline arguments to the program, and  argv  array contains those arguments. The actual arguments start from index 1. I/O Streams cout  is the stream for standard output,  cerr  is the stream for standard error. The  <<  operator lets one