Kaznov
Kaznov

Reputation: 1163

QTabWidget - how to "include" a pointer to every tab?

I'm trying to make a simple communicator, with UI based on tabs (QTabWidget). I want tabs to be closeable and movable. Still, for every tab I would like to remember a pointer to my class (where I keep socket etc.), so I could manage closing tabs and disconnecting sockets.

One way is to keep them(pointers) in array / any container, analyze any move that was done by a user, and change indexes or swap pointers dependently on index of tabs, that were moved, but this involves a lot of work, and even more bugs. Is there any other and simpler way I could get it?

Upvotes: 0

Views: 233

Answers (1)

coyotte508
coyotte508

Reputation: 9745

Use myTabWidget->widget(index).

There is one for each tab.

Doc

You can set the widget as the parent of your class if your class inherits from QObject, or connect its signals (like destroyed()) with that of your class.

Or you can even do

QVariant prop = QVariant::fromValue<intptr_t>((intptr_t)workerObject);
myTabWidget->widget(index)->setProperty("workerObject", prop);

to really store the pointer, and

QVariant prop =  myTabWidget->widget(index)->getProperty("workerObject");
WorkerClass *ptr = (WorkerClass*) prop.value<intptr_t>();

to get it back.

Upvotes: 1

Related Questions