October 5, 2010

szh1 szh1
Lab Rat
126 posts

“this->setEnabled(false);” not always disabling window

 

I am working on a batch image format converter. I have some code:

  1.     // disable the window
  2.     this->setEnabled(false);
  3.     if (outputDir != QString::null)
  4.     {
  5.         // iterate through the items in imageList
  6.         for (int row = 0; row < this->ui->imageList->count(); row++)
  7.         {
  8.             // create a QListWidgetItem from the row
  9.             QListWidgetItem *item = this->ui->imageList->item(row);
  10.  
  11.             // check to make sure the file exists
  12.             if (!QFile(item->text()).exists())
  13.             {
  14.                 QMessageBox::warning(this, "Error", "Cannot find file '" + item->text() + "'. Check to make sure the file exists.");
  15.                 continue;
  16.             }
  17.  
  18.             // convert the image with the selected settings
  19.             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)
  20.             {
  21.                 // if the image was converter and the user selected deleteOriginalCheckBox, delete the original file
  22.                 QFile::remove(item->text());
  23.             }
  24.         }
  25.     }
  26.     else
  27.     {
  28.         // iterate through the items in imageList
  29.         for (int row = 0; row < this->ui->imageList->count(); row++)
  30.         {
  31.             // create a QListWidgetItem from the row
  32.             QListWidgetItem *item = this->ui->imageList->item(row);
  33.  
  34.             // check to make sure the file exists
  35.             if (!QFile(item->text()).exists())
  36.             {
  37.                 QMessageBox::warning(this, "Error", "Cannot find file '" + item->text() + "'. Check to make sure the file exists.");
  38.                 continue;
  39.             }
  40.  
  41.             // convert the image with the selected settings
  42.             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)
  43.             {
  44.                 // if the image was converter and the user selected deleteOriginalCheckBox, delete the original file
  45.                 QFile::remove(item->text());
  46.             }
  47.         }
  48.     }
  49.     // enable the window
  50.     this->setEnabled(true);
  51. }

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.

12 replies

October 5, 2010

Luca Luca
Ant Farmer
635 posts

try with:

  1. QApplication::processEvents();

after the:

  1. this->setEnabled(false);

October 5, 2010

szh1 szh1
Lab Rat
126 posts

Thank you! It works perfectly.

October 5, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
377 posts

The best way is usage another thread for converting (outside gui thread).

October 5, 2010

MTK358 MTK358
Lab Rat
27 posts

You don’t have to write “this->” to access class members in C++.

October 5, 2010

szh1 szh1
Lab Rat
126 posts

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++?

October 6, 2010

MTK358 MTK358
Lab Rat
27 posts
szh1 wrote:
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++?

Never heard of such a thing (except for languages like Ruby and Python, which require you to do so).

October 6, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3386 posts

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.

October 6, 2010

VC15 VC15
Lab Rat
25 posts

As far as I know if there is a member function and a global function with the same signature and it is called from within a class the compiler always choose the member.

October 6, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3386 posts

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:

  1. class A {
  2. public:
  3.    A() : something(5) {}
  4.  
  5.    void method()
  6.    {
  7.         int something = 7;
  8.         qDebug() << something << this->something;
  9.    }
  10.  
  11. private:
  12.     int something;
  13. };

October 6, 2010

VC15 VC15
Lab Rat
25 posts

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.

October 6, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3386 posts

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;-).

October 6, 2010

szh1 szh1
Lab Rat
126 posts

So I guess it is just preference.

 
  ‹‹ Static function getOpenFileName() of QFileDialog does not produce a native filedialog on KDE4 when linked static.      [Solved] double setAlphaChannel ››

You must log in to post a reply. Not a member yet? Register here!