PDA

View Full Version : Very new to QT-Creator struggling with combobox and push button



MrCrackPotBuilder
28th June 2017, 09:30
Hi guys im struggling with making a push button reset 6 different (but all horizontally aligned) widgets. the code im currently using is as follows:


void MainWindow::on_button_clicked()
{
ui->lineEdit1->clear();
ui->lineEdit2->clear();
ui->combobox1->clear();
ui->lineEdit3->clear();
ui->lineEdit4->clear();
ui->combobox2->clear();
}



The line edits clear perfectly but the comboBoxes delete all the information I have previously keyed in. How would I instead have the pushButton reset the combobox back to the original starting selection.

eg: --- is the standard then 1, 2, 3, so i would want to go back to --- but still be able to select 1, 2, 3


Second question would be I have to do this for 20 lines, all the same as above only labeled 1 - 20 eg tMT1 - tMT20, bTN1 - bTN20. Is there a way to make the code DRY ????

Added after 1 2 minutes:

Figured it out correct code is

ui->combobox1->setCurrentIndex(WhatEverTheOriginalWas);

UPDATE

DO NOT type the selection i.e mine is ---, 1, 2, 3, etc it is 0 for ---, 1 for 1, 2 for 2, and so on

code would be

ui->combo1->setCurrentIndex(0);

(mine is 0 because i want --- to be displayed)

MrCrackPotBuilder
28th June 2017, 11:04
I still need help to DRY the code as i now have whats above repeated 20 times just with the ending number changing if anyone knows how to it would be amazing

Lesiok
28th June 2017, 11:39
Read about QObject::findChildren() especially the version with QRegExp.

MrCrackPotBuilder
28th June 2017, 13:21
Thanks a bunch once i figure it out i will again update the thread with process and code :)

d_stranz
28th June 2017, 17:00
An alternative discussed in another thread is to use Qt Designer to create your UI, and then at startup after calling setupUi() you copy the pointers into QVector< QPushButton * >, etc. See this thread. (http://www.qtcentre.org/threads/68327-Can-we-create-an-array-of-widgets)