Solved ui->addTab "index out of range" !?
-
Hey,
I have a normal mainwindow class, where I call in the constructor a function.
This function is supposed to add a new tab with a custom widget from another class to the tab widget.
Problem here: When I run this code, the program crashes at this line:ui->tabGraph->addTab( myNewGraphWidget, "test");
with this error:
ASSERT failure in QVector<T>::operator[]: "index out of range"
I do not even use a vector here...
Any ideas?
Thanks for answers -
@Niagarer
Super. Found itWhen you insert new tab. A signal is sent (currentChanged)
you catch signal ( its auto connected )
on_Graph_currentChanged(int index)
and then kill yourself with allNodesWidgets[index] as index is 100% surely out of bounds as nothing added to
allNodesWidgets yet.void MainWindow::on_Graph_currentChanged(int index)
{
ui->allNodes_scrollArea->setWidget( allNodesWidgets[index] ); <<<< this gives the actual error
} -
Hi,
Can you provide a stack trace of your crash ?
-
@SGaist
uuuhhm..
I'm sorry, can you give me a link or sth. where I can see how...
Unfortunately don't really know what you mean... (do you men an exeption output? ) -
What platform are you on ?
-
@SGaist
windows 10 x64 -
MinGW or Visual Studio ?
-
@SGaist
MinGW 32bit -
Then you already have a debugger. Just start your application in debug mode, make it crash and you'll have the stack trace.
-
@SGaist
There is nothing under Stack when I am debugging...
Are my settings wrong? I can't find anything wrong.
Also nothing under Breakpoints, Threads, Modules, Source Files, Snapshots and Registers.
Only the Debugger Log shows content -
Silly question but are you starting the application as usual or are you starting it with the other button which means "debug the application" ?
-
@SGaist
No, I click Start Debugging
(but as I see now, there is Output in Modules tab when I am not closing the error message but it doesn't matter I think) -
@SGaist
UPDATE
Ok now I know, there are two points in my function, where the program crashes.
My function:GraphWidget *newGraphWidget = new GraphWidget(this); graphWidgets.append(newGraphWidget); QString name = "tab " + QString::number(tabCounter); qDebug() << ui->Graph->currentIndex(); //ui->Graph->addTab(newGraphWidget, "title"); // !? PROBLEM !? qDebug() << ui->Graph->currentIndex(); tabCounter += 1; QVBoxLayout *vBoxLayout = new QVBoxLayout(); vBoxLayout->setAlignment(Qt::AlignTop); QWidget *allNodesWidget = new QWidget(); allNodesWidget->setLayout(vBoxLayout); //ui->allNodes_scrollArea->setWidget( allNodesWidgets[ui->Graph->currentIndex()] ); // !? PROBLEM !? allNodesWidgets.append(allNodesWidget);
The two lines where I commented !? PROBLEM !? are the two lines. If I uncomment one of these, the program crashes with the Index error. (UPDATE: the second one is because ui->Graph->currentIndex() is -1)
I have really no Idea what this means...
Help please! -
What is the initial value of tabCounter? What is the vector size?
-
This post is deleted! -
allNodesWidgets[ui->Graph->currentIndex()]
ui->Graph->currentIndex()
is almost certainly out of bounds -
@VRonin
Oh, yes you are right, thanks.
But it unfortunately does not solve teh actual problem :/ -
@Vinod-Kuntoji
The initial value of TabCounter is 0.
The vector has no specific size, it gets bigger when I add new items... what exactely do you mean? -
@Niagarer when you add new tab it do not mean that current index will be changed
-
@Paul-Busovikov
Yes...?
To get sure I wroteqDebug() << ui->Graph->currentIndex();
before and after adding a new tab
-
To make it easier:
the lineui->Graph->addTab(new QWidget(this), "hello");
does not work. Doesn't matter where I call it.