PDA

View Full Version : Adding widgets without using any layout. Is it possible?



yatazuo
23rd August 2010, 12:04
Hi all,
i'm new on Qt. I'm writing my own custom control extending the QWidget class. I need to put buttons on my control (like 1000 or 2000) and i would use the QPushbutton class.
Is there a way to add buttons and labels to my custom control without putting them into any layout?

Thanks in advance.

dpatel
23rd August 2010, 12:09
You can place those buttons and labels on a QFrame (as container). You will have to setGeometry() for each button and label. I think it should also work without using QFrame in which case you will have to pass some parent widget.

squidge
23rd August 2010, 12:14
I would advise against adding 1,000 - 2,000 QPushbutton's to a QWidget-derived object. Is there a reason why you need to use that many buttons?

yatazuo
23rd August 2010, 12:48
You will have to setGeometry() for each button and label. I think it should also work without using QFrame in which case you will have to pass some parent widget.

I created my buttons and called setGeometry() for each one and It works. Many thanks.


Is there a reason why you need to use that many buttons?

I need to control the same number of objects. I'm implementing a grid with 1000-2000 rows and i would put a button on each one for hiding or showing.

Maybe a better solution would be to draw the same amount of QImages and appropriately dispatch mouse events..

yakin
23rd August 2010, 15:16
If you think about 2000 buttons keep the application performance in mind. All these objects have to be handled by the process with all their events and render schedules.
Can I make a suggestion:
Why don't you use a TableView with a button delegate. You can call "QAbstractItemView::setItemDelegate (...)" to set your own delegate. In the delegates paint method, you can simply paint a button with QStyleOptionButton and handle the events you need by yourself. What you got is a TableView filled with Buttons without having the overhead of the complex QPushButton instance. You will find example code in the Qt- Documentation.

yatazuo
23rd August 2010, 17:29
Like i said, i'm new on qt. Didn't know about the Qtableview.
Each row of the grid can contain a non fixed number of other controls (other buttons, custom controls, ecc).
Does the Qtableview allows to put Qwidgets on the table cells?


You will find example code in the Qt- Documentation.

Can you please post the link?

Thanks

yakin
23rd August 2010, 17:56
The Star Delegate Example should be a good start. http://doc.qt.nokia.com/4.6/itemviews-stardelegate.html

However, the QTableView has a defined column count. You cannot say that one row has a column count of 5 and the other a count of 3 for example. And it is quite complicate to show different controls for each field in the table (not imposible but tricky. Particularly if you want different controls in a row respectively in a column, because it is possible to set a delegate for a row / column but not for a field).

Nevertheless I think you would not be lucky with 2000 constructed buttons / complex controls due to the performance issue.