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. Compose pointer to property of object
QtWS25 Last Chance

Compose pointer to property of object

Scheduled Pinned Locked Moved Solved General and Desktop
c++pointerobjectsproperties
25 Posts 3 Posters 8.2k Views
  • 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.
  • M Offline
    M Offline
    McLion
    wrote on 1 Apr 2016, 11:40 last edited by
    #1

    Hi

    I need to have multiple instances of an object, i.e. like:
    ui->webGUI01
    ui->webGUI02
    ui->webGUI03
    ...
    that I need to set properties from an external interface like:
    ui->webGUI01->show
    ui->webGUI02->show
    ui->webGUI03->show
    ...

    I get the ID# as an int value. Is there a way I can 'compose' these pointers in code like concatenate strings?
    Otherwise I don't see any other option as to store these object/property pointers in an array and load by the index if I don't want have huge (and fixed) iterations in code.

    Do I miss something or is there a better solution?
    Thanks
    McL

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Apr 2016, 12:43 last edited by
      #2

      well
      Not sure what
      " pointers in code like concatenate strings?"
      would be, but you can easy make list with all such pointers

      QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();

      if you replace "QPushButton *" with the type of webGUI03
      (keep *) then you have a list of them.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        McLion
        wrote on 1 Apr 2016, 13:12 last edited by
        #3

        Thanks.
        The idea is not to have something like this:

        char ShowGUIid(int ID) {
         switch(ID)
          case 01: ui->webGUI01->show();
          case 02: ui->webGUI02->show();
          ...
          case n: ui->webGUIn->show(); }
        

        but maybe something like (shown here as if it were strings - I know it doesn't work):

        char ShowGUIid(int ID) {
        'ui->webGUI' + ID + '->show()'; }
        

        Get the idea?
        I'd need this for setting various properties like url's, size, pos, visibility ...
        Second idea would make it easy to add additional webGUI's as needed, dynamically at runtime and is much less code.

        Maybe there is a much better way of doing this?!?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Apr 2016, 13:20 last edited by mrjj 4 Jan 2016, 13:20
          #4

          but if you put in list u can do

          for (int dx=0; dx < allPButtons.size(); dx++ )
          allPButtons[dx]->show();

          or
          allPButtons[ID]->show();
          allPButtons[ID]->WhatEverProperty();

          and then u dont need the switch case. ?

          M 2 Replies Last reply 1 Apr 2016, 13:24
          1
          • M mrjj
            1 Apr 2016, 13:20

            but if you put in list u can do

            for (int dx=0; dx < allPButtons.size(); dx++ )
            allPButtons[dx]->show();

            or
            allPButtons[ID]->show();
            allPButtons[ID]->WhatEverProperty();

            and then u dont need the switch case. ?

            M Offline
            M Offline
            McLion
            wrote on 1 Apr 2016, 13:24 last edited by
            #5

            @mrjj
            Thanks.
            I'll try to implement your idea .. and get back with the result ( or more questions ;-)

            1 Reply Last reply
            1
            • M mrjj
              1 Apr 2016, 13:20

              but if you put in list u can do

              for (int dx=0; dx < allPButtons.size(); dx++ )
              allPButtons[dx]->show();

              or
              allPButtons[ID]->show();
              allPButtons[ID]->WhatEverProperty();

              and then u dont need the switch case. ?

              M Offline
              M Offline
              McLion
              wrote on 4 Apr 2016, 08:39 last edited by
              #6

              @mrjj
              I got stock at some other place.
              What's the best way to create the webGUI's (qWebView) at run-time, only as needed?
              I mean, I always need one at least, but only sometimes more than one and was thinking of not using up the memory by preparing a defined number of copies.
              Any hint's, if possible at all?
              Thanks.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 4 Apr 2016, 08:55 last edited by
                #7

                hi
                its always possible to NEW widgets on demand.
                How you will do it, mostly depends tón the GUI.
                How user will do it.

                At any time you can do
                QWebView *view = new QWebView(parent);
                view->load(QUrl("http://qt-project.org"));
                view->show();

                M 1 Reply Last reply 4 Apr 2016, 09:26
                0
                • M mrjj
                  4 Apr 2016, 08:55

                  hi
                  its always possible to NEW widgets on demand.
                  How you will do it, mostly depends tón the GUI.
                  How user will do it.

                  At any time you can do
                  QWebView *view = new QWebView(parent);
                  view->load(QUrl("http://qt-project.org"));
                  view->show();

                  M Offline
                  M Offline
                  McLion
                  wrote on 4 Apr 2016, 09:26 last edited by
                  #8

                  @mrjj
                  Here's what I'm doing:

                    QWebView *webGUI = new QWebView(this);
                    webGUI->setObjectName(QString::fromUtf8("webGUI"));
                    webGUI->setGeometry(QRect(280, 190, 150, 120));
                    webGUI->setAcceptDrops(false);
                    webGUI->setStyleSheet(QString::fromUtf8("background-color: rgba(127, 127, 127, 0);"));
                    webGUI->setUrl(QUrl("about:blank"));
                    webGUI->setRenderHints(QPainter::SmoothPixmapTransform|QPainter::TextAntialiasing);
                    webGUI->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
                    webGUI->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
                    webGUI->setGeometry(0, 0, SCREEN_SIZE_X, SCREEN_SIZE_Y);
                  

                  This is in a separate function and does not yet have the code to make it create webGUI[01] .. webGUI[n]. I thought of trying with one first.
                  I currently call this from the main constructor.

                  Before, the concept used only one webGUI that I created in the Designer - everything worked as supposed.

                  I'm accessing these webGUI's from various places in my code.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 4 Apr 2016, 09:31 last edited by
                    #9

                    hi
                    If you make this into a fine function that returns a QWebView *, you
                    can easy make more than one.

                    I do wonder how the parent QWebView(this); << the "this" can
                    show mutiple QWebView ?
                    will you use a tab control or a stackedwidetget to allow user to flip between the
                    WEbViews or ill the be in same page and just take a smaller share of it ?
                    as in share the page/parent ?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      McLion
                      wrote on 4 Apr 2016, 09:45 last edited by McLion 4 Apr 2016, 09:46
                      #10

                      'This' might be one of my problems.
                      While in Designer I had a centralWidget as parent for all my elements.
                      The webGUI's are dynamically loaded with content from a local server and are shown/hidden as needed. They also are placed and sized as needed at run-time and cover all of the screen or or only parts, may overlap with transparency ...
                      There is at least one, but should now be extendable on request.

                      Am I correct that I will need to create webView01 from the constructor at startup and make it available in *.h. Otherwise all the references to it are not satisfied.

                      All references/calls to any webGui need to make use pof the QList, right.

                      As you may surely have noted, I never implemented such a logic/functionality before.
                      Thanks a lot for your help and guidance!

                      M 1 Reply Last reply 4 Apr 2016, 10:21
                      0
                      • M McLion
                        4 Apr 2016, 09:45

                        'This' might be one of my problems.
                        While in Designer I had a centralWidget as parent for all my elements.
                        The webGUI's are dynamically loaded with content from a local server and are shown/hidden as needed. They also are placed and sized as needed at run-time and cover all of the screen or or only parts, may overlap with transparency ...
                        There is at least one, but should now be extendable on request.

                        Am I correct that I will need to create webView01 from the constructor at startup and make it available in *.h. Otherwise all the references to it are not satisfied.

                        All references/calls to any webGui need to make use pof the QList, right.

                        As you may surely have noted, I never implemented such a logic/functionality before.
                        Thanks a lot for your help and guidance!

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 4 Apr 2016, 10:21 last edited by
                        #11

                        @McLion said:

                        • 'This' might be one of my problems.
                          Well "this" is just the parent. if no parent are giving, it will become a window.
                          So often this will be mainwindow. There nothing wrong having mutiple
                          WebViews in same parent but u might need to arrange them to not overlap.
                          (use move or setGeometry) Since it sounds you will arrange them manually and not use a layout or tabs or something like that.

                        • Am I correct that I will need to create webView01 from the constructor at startup and make it available in *.h. Otherwise all the references to it are not satisfied.
                          Well you dont need that.
                          You can have list
                          QList< QWebView *> Webs;
                          QWebView *webGUI = new QWebView(this);
                          ...
                          Webs.append(webGUI); // keep in list

                        Then at any time
                        QWebView *w = Webs[0]; // take from list
                        or Webs[1] , 2,3,4 etc.

                        So you dont need a named variable to access it.

                        Also, handling signals. You a can use same slot for all webviews.
                        the sender() function will tell you which webview
                        QWebView * wv= qobject_cast<QWebView *>(sender());
                        if (wv) ...
                        inside the slot for a signal to know which of the webviews that send the signal.
                        Not sure you will need it, so just a note :)

                        1 Reply Last reply
                        1
                        • M Offline
                          M Offline
                          McLion
                          wrote on 5 Apr 2016, 09:14 last edited by McLion 4 May 2016, 09:15
                          #12

                          Getting forward :-)
                          The hint with the sender in the signal was great too. As if you would know before, I really stumbled over this - thanks!

                          I came across an issue with the list though.
                          There are no absolute positions in the list. Saying if 3 entries are made (0,1,2) and the second list (1) is cancelled for instance because it's not needed anymore, former entry 2 becomes entry 1.
                          QList seems to always fill from the 0 up and there are no absolute positions.

                          I either did not get all of QList or I may need to switch to some other solution - unfortunately- because it otherwise works a treat.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 5 Apr 2016, 09:40 last edited by mrjj 4 May 2016, 09:49
                            #13

                            hi
                            Im not sure what wrong with QList since its dynamic and
                            we cant have invalid pointers around ?
                            else use a map
                            http://doc.qt.io/qt-5/qmap.html
                            QMap<int,QWebView * > map;
                            QWebView *webGUI = new QWebView(this);
                            ...
                            map[1] =webGUI ;
                            map[2] =nextwebGUI ;
                            ...
                            QWebView *w=map[2]; // it will stay "2"

                            They wont change position as 1,2 is a key
                            like map["onekey"] =webGUI ;
                            but we use int as no need for string.
                            so even if we remove map[1] then
                            map[2] is still there.

                            So maybe it works better for you ?

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              McLion
                              wrote on 5 Apr 2016, 09:58 last edited by
                              #14

                              Tried quickly ... and the Map is exactly what I need.
                              Thanks a million! .... I'll be back ;-)

                              M 1 Reply Last reply 5 Apr 2016, 10:02
                              0
                              • M McLion
                                5 Apr 2016, 09:58

                                Tried quickly ... and the Map is exactly what I need.
                                Thanks a million! .... I'll be back ;-)

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 5 Apr 2016, 10:02 last edited by
                                #15

                                @McLion
                                super
                                notice if you reuse the key
                                map[1] =webGUI ;
                                map[1] =nextwebGUI ;
                                It will replace the web *.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  McLion
                                  wrote on 5 Apr 2016, 10:03 last edited by
                                  #16

                                  Thanks for the heads-up.
                                  I'll do some testing and experiment.

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    McLion
                                    wrote on 5 Apr 2016, 14:16 last edited by
                                    #17

                                    So far, everything related to this threads question and the QMap as solution seems to work.

                                    I stumbled over something else while relacing the QwebView from the Designer by my 'on-demand' created one:
                                    I can not load an url, it crashes with sig11.

                                    I get the following connect error on bootup:

                                    QMetaObject::connectSlotsByName: No matching signal for on_webGUI_loadFinished(bool)
                                    

                                    which I dont understand and may be the cause.
                                    I have connected:

                                    connect(webGUI, SIGNAL(loadFinished(bool)), this, SLOT(on_webGUI_loadFinished(bool)));
                                    

                                    and in *.h as private slot:

                                    void on_webGUI_loadFinished(bool arg1);
                                    

                                    This worked before, with the QWebView from the Designer.
                                    Any idea? Thanks.

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 5 Apr 2016, 14:26 last edited by
                                      #18

                                      @McLion said:

                                      no matching signal for on_webGUI_loadFinished(bool)

                                      Hi
                                      this warning comes as you name slot on_XXX
                                      This will trigger Qt auto connect feature so when you use
                                      a concrete connect you should rename it so it dont
                                      start with on__
                                      (right click it- refactor->rename)

                                      also please do
                                      qDebug() << "loadfin:" << connect(webGUI, SIGNAL(loadFinixxxx

                                      to see if it returns true; ( as in , it can connect)

                                      1 Reply Last reply
                                      1
                                      • M Offline
                                        M Offline
                                        McLion
                                        wrote on 5 Apr 2016, 14:39 last edited by
                                        #19

                                        I wasn't aware that using on_ in the name will trigger any functionality!
                                        Refactoring solved this problem - Thanks !

                                        However, the signal 11 crash when trying to load an url seems to have some other cause .... I'm searching ;-)

                                        1 Reply Last reply
                                        0
                                        • jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 5 Apr 2016, 15:47 last edited by
                                          #20

                                          You're most probably dereferencing a null pointer. Signal 11 means SIGSEGV (segmentation error).

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          M 1 Reply Last reply 6 Apr 2016, 08:21
                                          0

                                          1/25

                                          1 Apr 2016, 11:40

                                          • Login

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