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. declare pointers to controls in .ui file
Forum Updated to NodeBB v4.3 + New Features

declare pointers to controls in .ui file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 310 Views 2 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.
  • B Offline
    B Offline
    Bryan Kelly
    wrote on last edited by Bryan Kelly
    #1

    The project has a dialog created as a class, and has files: quadratic.h, .cpp, and .ui. Is this sufficient to describe the situation?

    Just in case, with an existing project did this:

    File-> New File -> Qt -> Qt Designer Form Class
    

    In the new dialog, ten QDoubleSpinBox controls were added.

    What is the syntax to create a pointer to the controls in the class?
    Here are some test pointer attempts, all solicited errors.
    This is in quadratic.h
    Items beginning with dspin_q are existing controls in quadratic.ui

    QDoubleSpinBox * test1 =      dspin_q_input_a;
    QDoubleSpinBox * test2 =  ui->dspin_q_input_a;
    QDoubleSpinBox * test3 =     &dspin_q_input_a;
    QDoubleSpinBox * test4 = &ui->dspin_q_input_a;
    

    Here is the basic concept of the final declaration, obviously incorrect.

    static const int dspin_count = 10;
    QDoubleSpinBox * dsb_array[ dspin_count ] =
    {
       dspin_q_input_a, dspin_q_input_b, dspin_q_input_c,
       dspin_q_res_pos, dspin_q_res_neg,
       dspin_q_t1, dspin_q_t2, dspin_q_t3, dspin_q_t4, dspin_q_t5
    };
    

    Here is the basic concept of the goal for all this. This will go in quadratic.cpp

    for( int count = 1, count < dspin_count, count ++ )
    {
       dsb_array[ count ]->setMinimum( std::numeric_limits<double>::min() ); 
       dsb_array[ count ]->setMaximum( std::numeric_limits<double>::max() ); 
       dsb_array[ count ]->setButtonSymbols( QAbstractSpinBox::NoButtons );  // edit, just found this
    }
    

    I asked ChatGPT for this but it persists in creating the control with a new object rather than referencing an existing control from the dot .ui file. How might that question be worded to ChatGPT?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      test2 is the correct variant.

      Don't use a hardcoded array, use a QList so if you add or remove widgets, you only have to update the QList.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        test2 is the correct variant.

        Don't use a hardcoded array, use a QList so if you add or remove widgets, you only have to update the QList.

        B Offline
        B Offline
        Bryan Kelly
        wrote on last edited by Bryan Kelly
        #3

        @SGaist Item test2 produces an error, as do all the remainder.
        test2.png

        Everything was commented out while trying to make progress in other areas.

        Qlist? Looking into that.

        JonBJ 1 Reply Last reply
        0
        • B Bryan Kelly

          @SGaist Item test2 produces an error, as do all the remainder.
          test2.png

          Everything was commented out while trying to make progress in other areas.

          Qlist? Looking into that.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Bryan-Kelly
          test2 is the only correct one. Did you read or click on the "fix available" (that means on the light bulb), or ignore it? It tells you that QDoubleSpinBox has not been declared, and you need to fix that by including the header file per the docs, not that the statement itself is inherently incorrect.

          B 1 Reply Last reply
          1
          • JonBJ JonB

            @Bryan-Kelly
            test2 is the only correct one. Did you read or click on the "fix available" (that means on the light bulb), or ignore it? It tells you that QDoubleSpinBox has not been declared, and you need to fix that by including the header file per the docs, not that the statement itself is inherently incorrect.

            B Offline
            B Offline
            Bryan Kelly
            wrote on last edited by Bryan Kelly
            #5

            @JonB I don't understand. If QDoubleSpinBox has not been declared, then how can I drop those controls onto the dialog?

            Yes, I see the error message. Left and center click it and nothing happened. Right click to new a new long dialog.

            Side question: How is that dialog dismissed?

            Regarding that dialog, clicked a few selections and did not find an explanation. Which item needs to be selected?

            Unexpected: Clicked on the light bulb of the image shown in my previous post, and the message disappeared. Now that line, the test2 line, compiles and the program runs. I cannot detect any difference in the code. I don't know why the error message closed out.

            Edit: still working QList.

            SGaistS 1 Reply Last reply
            0
            • B Bryan Kelly

              @JonB I don't understand. If QDoubleSpinBox has not been declared, then how can I drop those controls onto the dialog?

              Yes, I see the error message. Left and center click it and nothing happened. Right click to new a new long dialog.

              Side question: How is that dialog dismissed?

              Regarding that dialog, clicked a few selections and did not find an explanation. Which item needs to be selected?

              Unexpected: Clicked on the light bulb of the image shown in my previous post, and the message disappeared. Now that line, the test2 line, compiles and the program runs. I cannot detect any difference in the code. I don't know why the error message closed out.

              Edit: still working QList.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Bryan-Kelly Designer does not care about headers, it's a visual tool.

              You want to use a class, you need to include its header where you use it. Check the includes at the top of your cpp file.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

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