0

I've recently started lerning Qt and I'm facing the following problem:


I want to create sth like a chessboard (empty, doesn't have to do anything for now) - I've drawn a simple interface, but in the middle I need to make a chessboard itself (let's say it will be made out of small QTextBrowsers).

The problem is that the size of chessboard must be specified by user. So, drawing 16 fields and giving them unique objectNames is easy, but I have no idea how to:

  1. generate those fields 'dynamically'
  2. generate unique names for them, so I will be able to refer to them later in code eg. field_1_1, field_1_2, field_1_3

Thans in advance,

Skipper
  • 775
  • 6
  • 17
  • 32
  • Why do they need names? – Leon Nov 26 '14 at 12:25
  • Why not to use simple QTableView and it's row/column-index navigation? I did what you want here few years ago by using tableview and custom cells. – VP. Nov 26 '14 at 12:28
  • @Leon ... Maybe they do not have to :D But I need to somehow be able to change them in code to use eg. setStyleSheet – Skipper Nov 26 '14 at 12:29
  • If you gonna hardcode like that `field_1_1` I have bad news for you.. Explore the things and technologies that Qt provides and think twice what you need to achieve and what from Qt can help you achieve your goal. – VP. Nov 26 '14 at 12:31

1 Answers1

2

So what I think you want is a two-dimensional array of fields. That way you can reference the correct field without knowing the name.

More specifically Qt provides QLayoutGrid which, although not a two dimensional array, will give you grid access to widgets by using the itemAtPosition method

Leon
  • 12,013
  • 5
  • 36
  • 59
  • Thanks a lot, I've created sth like You said. The thing is that itemAtPosition returns QLayoutItem, and my items are of type QTextBrowser, so is there a way to use their functions? – Skipper Nov 26 '14 at 13:58
  • so QLayoutItem has a method `widget()`, you will have to type cast it though – Leon Nov 26 '14 at 14:01