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. Create an array containing different types?

Create an array containing different types?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 510 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
    legitnameyo
    wrote on last edited by
    #1

    I've got a program that has four "windows" that are split in equal sizes inside the main window. They are randomly assigned either a type of QFrame or QTextEdit. My issue is that I either need to have a super long, messy code nest with tons of if statements to reference them correctly OR I need a vector of some sort. The issue is that I can't create a vector with BOTH QFrame and QTextEdit references inside. My question is therefore, is there a way to create an array/vector/etc containing both QFrame and QTextEdit? I've tried creating a vector array of type "any" with boost, but boost does not install on my computer for some reason

    std::vector vector_of_windows<boost::any>; // does not work since I am using a mac that does not have boost installed (I've tried using homebrew, Mac ports and building it from source myself. Still does not work.)
    

    All I want to achieve is the following

    stack_of_windows[i]->resize(window_width, window_height);
    
    JKSHJ J.HilkJ 2 Replies Last reply
    0
    • L legitnameyo

      I've got a program that has four "windows" that are split in equal sizes inside the main window. They are randomly assigned either a type of QFrame or QTextEdit. My issue is that I either need to have a super long, messy code nest with tons of if statements to reference them correctly OR I need a vector of some sort. The issue is that I can't create a vector with BOTH QFrame and QTextEdit references inside. My question is therefore, is there a way to create an array/vector/etc containing both QFrame and QTextEdit? I've tried creating a vector array of type "any" with boost, but boost does not install on my computer for some reason

      std::vector vector_of_windows<boost::any>; // does not work since I am using a mac that does not have boost installed (I've tried using homebrew, Mac ports and building it from source myself. Still does not work.)
      

      All I want to achieve is the following

      stack_of_windows[i]->resize(window_width, window_height);
      
      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @legitnameyo said in Create an array containing different types?:

      The issue is that I can't create a vector with BOTH QFrame and QTextEdit references inside. My question is therefore, is there a way to create an array/vector/etc containing both QFrame and QTextEdit?

      QTextEdit indirectly inherits QFrame. So, you can store them both in a std::vector<QFrame*>.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      4
      • L legitnameyo

        I've got a program that has four "windows" that are split in equal sizes inside the main window. They are randomly assigned either a type of QFrame or QTextEdit. My issue is that I either need to have a super long, messy code nest with tons of if statements to reference them correctly OR I need a vector of some sort. The issue is that I can't create a vector with BOTH QFrame and QTextEdit references inside. My question is therefore, is there a way to create an array/vector/etc containing both QFrame and QTextEdit? I've tried creating a vector array of type "any" with boost, but boost does not install on my computer for some reason

        std::vector vector_of_windows<boost::any>; // does not work since I am using a mac that does not have boost installed (I've tried using homebrew, Mac ports and building it from source myself. Still does not work.)
        

        All I want to achieve is the following

        stack_of_windows[i]->resize(window_width, window_height);
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        hi @legitnameyo

        resize is a function of QWidget, QFrame and QTextEdit both derive from QWidget.

        So you can simply make an array/Vecotr/list of QWidget pointers, and you're good to go.

        If you later on want to access function of the derived class you can do that as well, simply use qobject_cast<QFrame*>(widgetPointer),

        the cast returns a nullptr if the cast fails. so make sure to check, if your mix different classes.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2
        • L Offline
          L Offline
          legitnameyo
          wrote on last edited by
          #4

          Created a vector of QFrame* worked!

          O 1 Reply Last reply
          0
          • L legitnameyo

            Created a vector of QFrame* worked!

            O Offline
            O Offline
            ofmrew
            wrote on last edited by
            #5

            @legitnameyo The problem you identified is the heterogeneous collection versus the homogeneous collection. Smalltalk had the former since is was late binding, but C++ is strongly typed early bound, so it only supports the later. In your case the classes were derived from the same superclass, so you were saved, but that will not always be the case.

            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