Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    6
    13
    10853
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      szh1 last edited by

      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&#40;item->text(&#41;&#41;.exists(&#41;)
              {
                  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&#40;item->text(&#41;&#41;.exists(&#41;)
              {
                  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.

      1 Reply Last reply Reply Quote 0
      • L
        luca last edited by

        try with:

        @
        QApplication::processEvents();
        @

        after the:

        @
        this->setEnabled(false);
        @

        1 Reply Last reply Reply Quote 0
        • S
          szh1 last edited by

          Thank you! It works perfectly.

          1 Reply Last reply Reply Quote 0
          • A
            alexander last edited by

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

            1 Reply Last reply Reply Quote 0
            • M
              MTK358 last edited by

              You don't have to write "this->" to access class members in C++.

              1 Reply Last reply Reply Quote 0
              • S
                szh1 last edited by

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

                1 Reply Last reply Reply Quote 0
                • M
                  MTK358 last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • T
                    tobias.hunger last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • V
                      VC15 last edited by

                      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.

                      1 Reply Last reply Reply Quote 0
                      • T
                        tobias.hunger last edited by

                        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;
                        };
                        @

                        1 Reply Last reply Reply Quote 0
                        • V
                          VC15 last edited by

                          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.

                          1 Reply Last reply Reply Quote 0
                          • T
                            tobias.hunger last edited by

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

                            1 Reply Last reply Reply Quote 0
                            • S
                              szh1 last edited by

                              So I guess it is just preference.

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post