PDA

View Full Version : QVector filled with unespected numbers



cdbean04
26th October 2015, 14:57
Hi,
I am creating a Qt application on raspberry pi. I have a serise of checkboxes that if they are checked add their given number (1-25) to a QVector named Bank_1. I decided to display elements of the Vector in a text editor to make sure they are what I expected. They were not. When I display Bank_1[0] the result is anywhere from 24 to 495192 (or similar). Below is my code.




MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QVector<int> Bank_1(QVector<int>(6));
}


void MainWindow::on_add_Relay_pushButton_clicked()
{

if(ui->bank_Select_comboBox->currentText()=="Bank 1")
{

Bank_1[0] = 1;
}
//right now I am trying to get the number to display but am still getting odd results

void MainWindow::displayVector(QVector<int> bank)
{
int i = bank[0];


QString s = QString::number(i);

ui->relay_1_lineEdit->setText(s);
}




If you need to see the .h file I can add it. Any help or explanation would be greatly appreciated.

anda_skoa
26th October 2015, 16:13
Do you initialize the vector somewhere?

It is actually strange this works at all, the access of element in line 17 should have triggered an assert since your vector has size 0.

Cheers,
_

jefftee
28th October 2015, 06:12
Any help or explanation would be greatly appreciated.
The Bank_1 you declare and initialize in line 7 is a local variable defined in the constructor that will go out of scope when the constructor ends. Do you also have Bank_1 declared as a class member variable?