PDA

View Full Version : Problems with adding new window



admkrk
27th April 2014, 04:03
Hi, First off let me apologize if this doesn't make much sense at first.

I might just be going about this all wrong, but I'm trying to have a second window open and use that for getting input. What I have works OK until I try to send the result back to the main window.

gui is my main class(QMainWindow) and list(QWidget) is my second class that opens when list_button is clicked.


void gui::on_list_btn_clicked()
{
list *mileage_list = new list;
mileage_list->populate_list(mileage);
mileage_list->show();
}

populate_list() works fine also as expected.


void list::populate_list(QVector<Entry> mileage)
{
QString list_item;
QString format_date;

ui->listWidget->addItem("Date Miles Gallons");

for(int i = 0; i < mileage.size(); ++i)
{
for(int j = 0; j < mileage[i].date.length(); ++j)
{
format_date.append(mileage[i].date[j]);
if(j == 1 || j == 3)
{
format_date.append("/");
}
}
list_item = format_date % " "
% QString::number(mileage[i].miles) % " "
% QString::number(mileage[i].gallons);
ui->listWidget->addItem(list_item);
format_date = "";
}
}

I could just add the QListWidget to the main window, but I'd rather have it a separate window to save space in the main one. The way its supposed to work is to let the user select a date from the list and pass the index back to the main window which obviously doesn't work as I have it.


void list::on_listWidget_itemClicked(QListWidgetItem *item)
{
int start_index = ui->listWidget->row(item);

//gui->set_index(start_index);
}



void gui::set_index(int index)
{
ui->start_date_txtBox->setText(mileage[index].date);
}

Besides the problem of sending the index back(I feel like I'm tripping over a crack in the sidewalk here), I don't like that the list needs to be closed separately and it should close with the main window if not done so first. Can someone suggest either a way to make this work as is or where I should look to get it working more efficiently. If I left something out(which I probably did), just let me know and I'll fill in the gaps.

Thanks

anda_skoa
27th April 2014, 12:43
For sending the index back to the main window I would suggest using a signal.

In the header for list add a signals section to the class declaration


signals:
void indexSelected(int index);

or any other name you'd like.

In on_listWidget_itemClicked() you emit the signal


emit indexSelected(start_index);


Make gui::set_index a slot by putting it into a slots section of the class, e.g.


private slots:
void set_index(int index);


Then connect at the place where you create the secondary window


connect(mileage_list, SIGNAL(indexSelected(int)), this, SLOT(set_index(int)));


As for closing the sub window when the main window closes: make it a QDialog subclass (instead of deriving from QWidget) and pass the main window as the dialog's parent



list *mileage_list = new list(this);


Cheers,
_

admkrk
27th April 2014, 19:35
Thanks! I've been having a hard time getting a handle on signals and slots and you just made it a lot clearer for me since I've been relying on Designer to handle that for me up to now.

I'll need to look into subclassing a bit more (I'm bad with terminology so it might just be a matter of, "oh you mean that" lol). I had already tried simply parenting it and that did not work. What I have works for now though and I'll worry more about that when I work on the cosmetics.