Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read access violation
Forum Updated to NodeBB v4.3 + New Features

Read access violation

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 490 Views 1 Watching
  • 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.
  • I Offline
    I Offline
    ikuris
    wrote on last edited by
    #1

    Hello,

    I have a list of checkbox ,

    QCheckBox* cblist[6];
    

    I am initializing that list in consturctor after setUI method

        ui->setupUi(this);
        cblist[0] = ui->checkBox;
        cblist[1] = ui->checkBox_2;
        cblist[2] = ui->checkBox_3;
        cblist[3] = ui->checkBox_4;
        cblist[4] = ui->checkBox_5;
        cblist[5] = ui->checkBox_6;
    

    I am changing their check state and color style in another slot function

        ui->checkBox->setChecked(true);
        ui->checkBox_2->setChecked(false);
        ui->checkBox_3->setChecked(true);
        ui->checkBox_4->setChecked(false);
        ui->checkBox_5->setChecked(true);
        ui->checkBox_6->setChecked(false);
    
        for(auto cbIndex = 0; cbIndex < 6; ++cbIndex)
        {
            if(cblist[cbIndex]->isChecked())
            {
                cblist[cbIndex]->setStyleSheet("QCheckBox { color : #55FF77; }");
            }
            else
            {
                cblist[cbIndex]->setStyleSheet("QCheckBox { color : #FF2222; }");
            }
        }
    

    The strange thing is when I am adding another widget from creator it does give me a error (0xC0000005: Access violation reading location). I am certain that I am not deleting or modifying that cblist array and I dont have a logical example or explanation, but I couldnt find anything and it does keep going like that.

    Thank you for your time and help.

    Christian EhrlicherC 1 Reply Last reply
    0
    • I ikuris

      Hello,

      I have a list of checkbox ,

      QCheckBox* cblist[6];
      

      I am initializing that list in consturctor after setUI method

          ui->setupUi(this);
          cblist[0] = ui->checkBox;
          cblist[1] = ui->checkBox_2;
          cblist[2] = ui->checkBox_3;
          cblist[3] = ui->checkBox_4;
          cblist[4] = ui->checkBox_5;
          cblist[5] = ui->checkBox_6;
      

      I am changing their check state and color style in another slot function

          ui->checkBox->setChecked(true);
          ui->checkBox_2->setChecked(false);
          ui->checkBox_3->setChecked(true);
          ui->checkBox_4->setChecked(false);
          ui->checkBox_5->setChecked(true);
          ui->checkBox_6->setChecked(false);
      
          for(auto cbIndex = 0; cbIndex < 6; ++cbIndex)
          {
              if(cblist[cbIndex]->isChecked())
              {
                  cblist[cbIndex]->setStyleSheet("QCheckBox { color : #55FF77; }");
              }
              else
              {
                  cblist[cbIndex]->setStyleSheet("QCheckBox { color : #FF2222; }");
              }
          }
      

      The strange thing is when I am adding another widget from creator it does give me a error (0xC0000005: Access violation reading location). I am certain that I am not deleting or modifying that cblist array and I dont have a logical example or explanation, but I couldnt find anything and it does keep going like that.

      Thank you for your time and help.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ikuris said in Read access violation:

      I am certain that I am not deleting or modifying that cblist array

      Don't be certain - check it by yourself by e.g. printing out the pointer values.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by Kent-Dorfman
        #3

        @ikuris said in Read access violation:

        I am changing their check state and color style in another slot function

        It looks ok but the thing that I'd suspect is that you are calling your slot function BEFORE the constructor is executed.

        Something else to think about: modern c++ standards.

        the more modern paradigm for handling arrays like you are creating is something like:

        std::vector<QCheckBox*> clist; // in class definition

        clist = { ui->cb1, ui->cb2, ui->cb3, ui-cb4 }; // in constructor

        and then your loops are controlled by clist.size();

        In this way you've removed the need for a MAX_INDEX value of checkboxes: a potential source of error.

        Here's another shortcut

        setStyleSheet(isChecked ? "true style" : "false style");

        I light my way forward with the fires of all the bridges I've burned behind me.

        1 Reply Last reply
        0
        • GerhardG Offline
          GerhardG Offline
          Gerhard
          wrote on last edited by
          #4

          @ikuris said in Read access violation:

          for(auto cbIndex = 0; cbIndex < 6; cbIndex++)

          Gerhard

          Kent-DorfmanK 1 Reply Last reply
          0
          • GerhardG Gerhard

            @ikuris said in Read access violation:

            for(auto cbIndex = 0; cbIndex < 6; cbIndex++)

            Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            @Gerhard

            pre or post increment makes no difference because the incrementing of loop variant is done at the bottom of the loop, not the top.

            I light my way forward with the fires of all the bridges I've burned behind me.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved