"this->setEnabled(false);" not always disabling window
-
I am working on a batch image format converter. I have some code:
@
// disable the window
this->setEnabled(false);
if (outputDir != QString::null)
{
// iterate through the items in imageList
for (int row = 0; row < this->ui->imageList->count(); row++)
{
// create a QListWidgetItem from the row
QListWidgetItem *item = this->ui->imageList->item(row);// check to make sure the file exists if (!QFile(item->text()).exists()) { QMessageBox::warning(this, "Error", "Cannot find file '" + item->text() + "'. Check to make sure the file exists."); continue; } // convert the image with the selected settings if (ImageConverter(NULL).Convert(item->text(), outputDir, QString(this->ui->outputFormatComboBox->currentText()), this->ui->qualitySpinBox->value(), this->ui->overwriteCheckBox->checkState() == Qt::Checked) && this->ui->deleteOriginalCheckBox->checkState() == Qt::Checked) { // if the image was converter and the user selected deleteOriginalCheckBox, delete the original file QFile::remove(item->text()); } } } else { // iterate through the items in imageList for (int row = 0; row < this->ui->imageList->count(); row++) { // create a QListWidgetItem from the row QListWidgetItem *item = this->ui->imageList->item(row); // check to make sure the file exists if (!QFile(item->text()).exists()) { QMessageBox::warning(this, "Error", "Cannot find file '" + item->text() + "'. Check to make sure the file exists."); continue; } // convert the image with the selected settings if (ImageConverter(NULL).Convert(item->text(), QString(this->ui->outputFormatComboBox->currentText()), this->ui->qualitySpinBox->value(), this->ui->overwriteCheckBox->checkState() == Qt::Checked) && this->ui->deleteOriginalCheckBox->checkState() == Qt::Checked) { // if the image was converter and the user selected deleteOriginalCheckBox, delete the original file QFile::remove(item->text()); } } } // enable the window this->setEnabled(true);
}
@to disable the window while the images are being converted. Strangely, the window only appears to be disabled in instances when I display a "QMessageBox" error message.
-
[quote author="szh1" date="1286314042"]MTK358:
I know, but in C# I learned that it is good practice to do so. I think it is because it makes it clearer if there are other functions with the same name. Is it different in C++?[/quote]Never heard of such a thing (except for languages like Ruby and Python, which require you to do so).
-
szh1: Yes, using this-> makes it clear you are referring to members, but a good IDE does the same thing using syntax highlighting. So why waste time on the extra typing?
MTK358: It is not required, but it makes clear that you want to access a member and not e.g. something global. It is perfect valid C++ and can even be necessary to do to disambiguate between things.
-
VC15: Yes, of course the compiler uses the scoping rules to resolve names. There are places where this->something and something can be different though. Try this:
@
class A {
public:
A() : something(5) {}void method()
{
int something = 7;
qDebug() << something << this->something;
}private:
int something;
};
@ -
I try to avoid such ambiguity. I realize that this is just an example. But in a real project to have a member and a local variable with the same name is not a good practice.
Of course, explicit pointing of scope increases readability. When you use this-> or type fully specified class name with the full chain of namespaces it belongs to you won't be surprised during runtime. But this also bloats your code and sometimes decreases readability. So as usual we have to find some compromise.
-
VC15: I fully agree with all you said.
I do prefer the best practice to avoid variables shadowing others to the best practice of putting "this->" before all member accesses, but both are valid ways to avoid confusing the programmer (not the compiler;-).