PDA

View Full Version : Cannot do addTab in loop for



Lokl
11th July 2016, 13:14
Hi everyone,

I want to instanciate as far as I want QWidget to push into QTabWidget.


QJsonArray jsonArray = jsonObject.value("Channel").toArray();

for(int indexchannel(0); indexchannel < jsonArray.size(); indexchannel++){
QString channel = jsonArray[indexchannel].toString();
// m_console.push_back(new Console(channel));
tab->addTab(new QWidget, channel);
m_console.append(new Console(channel));
onglet->setObjectName(QStringLiteral(channel));
tab->addTab(console, console->channelName() );
}
// for (int widgetIndexChannel(0) ; widgetIndexChannel <m_console.size(); widgetIndexChannel++ ){
// tab->addTab(m_console.at(widgetIndexChannel), m_console.at(widgetIndexChannel)->channelName());
// }
tab->addTab(new QWidget, "testttt");
verticalLayout->addWidget(tab);
this->setCentralWidget(centralWidget);


according to a json file I populate my view. But I'm doing this in loop and nothing append. I feel like the addTab doesn't work in loop even though doesn't it use like that does it ?
Sorry for my poor question.

d_stranz
11th July 2016, 16:50
I presume you are doing this in a MainWindow constructor.

- Why do you make the assumption that addTab() doesn't work in a loop, when you haven't even checked to see if the loop is actually being entered in the first place?

- How do you know that your json file has been opened and read correctly into the QJsonObject?

- You don't have any check to see that your QJsonArray instance actually -has- a size > 0

- What's "console"? A QWidget of some sort defined outside of the loop? If so, then why are you adding it again for every pass through the loop? Do you think the "new Console" call in line 7 is going to magically assign a new value to "console"?


Learn how to use a debugger. If you set a breakpoint at the beginning of this method, you could probably have learned immediately why your loop doesn't work as you expect.