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. Dynamic generation of QPushButtons
QtWS25 Last Chance

Dynamic generation of QPushButtons

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 6.7k 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.
  • A Offline
    A Offline
    amban
    wrote on last edited by
    #1

    Hi,
    I am a newbie. I wish to read contents from a file (say which has 5 names of fruits) and dynamically generate pushbuttons for each of them. I got the code working for writing/reading from a file. I am stuck in the 2nd step. I know how to create QPushButton statically and assigning signal/slots however how to generate these pushbutton based on the names read, how to go about it ?

    A simple program which generates say 10 pushbuttons in a 'for' loop which be very helpful.

    Thanks in advance.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      Hi,

      check you this article about "QPushButton":http://developer.qt.nokia.com/wiki/How_to_Use_QPushButton

      This tutorial demonstrates how to create a single QPushButton. You can modify it by adding a "for" loop and put instances of QPushButton to a QVector and you will solve your problem.

      Best regards,
      Leon

      http://anavi.org/

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

        And don't forget to put it into a layout. See the example for "QHBoxLayout":http://doc.qt.nokia.com/4.7/qhboxlayout.html on how to achieve this.

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          amban
          wrote on last edited by
          #4

          Hi,
          Thanks for all your help:) It works now. So i read a .txt file and generate pushbuttons for every line.

          Is there anyway to graphically connect these buttons generated ? It will look like a flowgraph where each pushbutton clicked will display a chunk of some code.

          I have been looking at QPainter where drawLine() seems like an option. However getting the co-ordinates of the pushbutton might be an issue.

          Thanks

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            [quote author="amban" date="1305057555"]
            Is there anyway to graphically connect these buttons generated ? It will look like a flowgraph where each pushbutton clicked will display a chunk of some code.
            [/quote]

            Yes, because "QPushButton":http://doc.qt.nokia.com/latest/qpushbutton.html class is fully customizable and you can modify the style of each button. Just check the documentation.

            Best regards,
            Leon

            http://anavi.org/

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Perhaps, if you want to create some kind of flow graph, you should look into other classes than QPushButton. Perhaps you should look into using a QGraphicsView. On the QGraphicsScene for that view, you create QGraphicsItems (one of the subclasses of that class that suit you). You can position them any way you like, and creating lines between them is as easy as creating QGraphicsLineItem instances for each line.

              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