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. QObject
Qt 6.11 is out! See what's new in the release blog

QObject

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.8k 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.
  • L Offline
    L Offline
    luca72
    wrote on last edited by
    #1

    Hello i have access through an object in this way:

    @
    for (int i =0; i<ui->tableWidget->rowCount();i++)
    {
    QWidget *cbox;
    QString nome_combo, nome_classe;
    cbox = new QWidget;
    cbox = ui->tableWidget->cellWidget(i,4);
    nome_combo = cbox->objectName();
    nome_classe = cbox->metaObject()->className(); \is qcombobox
    nomecl = nome_classe.toStdString();
    if (cbox->inherits(nomecl.c_str()))
    {
    here i need to use the function currentIndex() of the object, but how i can do it?
    }
    }
    @

    Thanks

    Luca

    EDIT: please use @-tags for code highlighting, Gerolf

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      @for (int i =0; i<ui->tableWidget->rowCount();i++)
      {
      QComboBox cbox = qobject_cast<QComboBox>(ui->tableWidget->cellWidget(i,4));
      if (cbox)
      {
      int index = cbox->currentIndex();
      }
      }@

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Hi luca72,

        first of all, you are creating new objects with
        @
        cbox = new QWidget;
        @
        and then overwrite the pointer with

        @
        cbox = ui->tableWidget->cellWidget(i,4);
        @

        so you create a memory leak. in your if statement, you have to cast.
        This is mostly basic C++ knowledge, which is needed to learn Qt.

        @
        QComboBox* cbobox = qobject_cast<QComboBox*>(cbox);
        @

        to shorten your code, you could do:

        @
        for (int i =0; i<ui->tableWidget->rowCount();i++)
        {
        QWidget cbox = ui->tableWidget->cellWidget(i,4);
        if(0 != cbox)
        {
        QComboBox
        cbobox = qobject_cast<QComboBox*>(cbox);
        if (0 != cbobox)
        {
        cbobox->currentIndex();
        //here i need to use the function currentIndex() of the object, but how i can do it?
        }
        }
        }
        @

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          Just a remark on Gerolf's suggestion:

          [quote author="Gerolf" date="1297092312"]to shorten your code, you could do:
          @
          for (int i =0; i<ui->tableWidget->rowCount();i++)
          {
          QWidget cbox = ui->tableWidget->cellWidget(i,4);
          if(0 != cbox)
          {
          QComboBox
          cbobox = qobject_cast<QComboBox*>(cbox);
          if (0 != cbobox)
          {
          cbobox->currentIndex();
          //here i need to use the function currentIndex() of the object, but how i can do it?
          }
          }
          }
          @[/quote]

          You're checking three times for null here (qobject_cast does it as well). It's not necessary and only clutters your code. The code can be even shorter as I suggested.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          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