Dynamic generation of QPushButtons
-
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.
-
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 -
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.
-
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
-
[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 -
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.