Learning C++ and Qt

April 02, 2013  •  1 Comment

C++ tips

  • It is a good practice to always assign values to variables when they are declared.  For example:  int x =0;

  • Functions should only perform one task.

  • When addressing compiler errors, always resolve the first error produced first.

  • Use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files.

  • Declare variables where they are needed (just in time).

  • If logical NOT is intended to operate on the result of other operators, the other operators and their operands need to be enclosed in parenthesis because NOT has very high operator precedence.

  • When dealing with bit operators, use unsigned variables.

  • Use delete[] to delete dynamically allocated arrays.

  • Assign pointers to 0 both when they are declared (unless assigned to another address), and after they are deleted. 

  • Always pass by constant reference unless your need to change the value of an argument.

  • Values returned by reference or pointer can be l-values or r-values. Values returned by value can only be r-values.

  • Handlers for derived exception classes should be listed before those for base classes.

 

Qt tips

  • When the project file is updated (.pro) make sure to run qmake to update the make file.

  • To add a main window class function slot to Builder right click on the QMainWindow object in the object inspector, select change signals and slots and add the function.  It will then appear in the signals and slots editor.

  • To add a slot to receive signals from a widget right click on the widget, select "Go to slot..." and select the signal.  A corrosponding slot function will be created in the MainWindow class.

 

 

Code Snippets

 


// Regular expression

QRegExp rx("^(Border|Text|Shape|Graphic)$");

if (rx.exactMatch(objectName) == true){
    // do something
}


// QString with arguments
statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
QString newTemplate = QString("New Template %1").arg(newTemplateCount);


// Algorithm to walk a tree structure
void DataModel::walkTree(const QModelIndex &row)
{
    if (this->hasChildren()) {
        for (int i = 0; i < rowCount(row); ++i){
            walkTree(this->index(i,0,row));
        }
    }
}
 

Comments

Muhammad Lal(non-registered)
Nice sharing! reading such article in an inspiring i like your post because its very informative.keep providing such kind of good post.once again thanks. C++ tips

It is a good practice to always assign values to variables when they are declared. For example: int x =0;

Functions should only perform one task.

When addressing compiler errors, always resolve the first error produced first.

Use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files.
href="http://www.technologyexplores.com/category/computer-and-

internet/graphics-cards/">computer</a>
No comments posted.
Loading...

Archive
January February March April May June (1) July August (1) September October (1) November December (1)
January February March (2) April (1) May June (1) July August (3) September October November (1) December
January February March April May (2) June July August September October (2) November December (1)
January (1) February March April May June July August September October November December
January February March April May June July (1) August (1) September (1) October November December
January February (2) March (1) April May June July August September October November December