If you are new to C++ then some of the compiler errors can be a bit confusing and daunting at first. For example if the compiler given you an expected primary-expression error (e.g. expected primary-expression before ‘.’ token), at face value it does not make sense.
void RGBViewer::accept() { QMessageBox::information(this, "Button Clicked", "You clicked OK", QMessageBox::Ok); QColor color = QColor(Ui_RGBViewerClass.spinBoxRed->value(), Ui_RGBViewerClass.spinBoxGreen->value(), Ui_RGBViewerClass.spinBoxBlue->value(), 255); qDebug() << color; QBrush brush(color); brush.setStyle(Qt::SolidPattern); ui.graphicsView->setBackgroundBrush(brush); ui.graphicsView->backgroundBrush(); }
When I compile the above code snippet, I get the following error from the compiler:
rgbviewer.cpp: In member function ‘virtual void RGBViewer::accept()’: rgbviewer.cpp:19: error: expected primary-expression before ‘(’ token rgbviewer.cpp:19: error: expected primary-expression before ‘.’ token rgbviewer.cpp:20: error: expected primary-expression before ‘.’ token rgbviewer.cpp:21: error: expected primary-expression before ‘.’ token
All this means is that I need to initialize the type ‘Ui_RGBViewerClass’ as the compiler does not understand it. In other words, I need to use a new.
Here is the ‘fixed’ version below of the same code snippet.
Ui::RGBViewerClass ui; //Defined in the header; shown here for completness void RGBViewer::accept() { QMessageBox::information(this, "Button Clicked", "You clicked OK", QMessageBox::Ok); QColor color = QColor(ui.spinBoxRed->value(), ui.spinBoxGreen->value(), ui.spinBoxBlue->value(), 255); qDebug() << color; QBrush brush(color); brush.setStyle(Qt::SolidPattern); ui.graphicsView->setBackgroundBrush(brush); ui.graphicsView->backgroundBrush(); }