Important: Please read the Qt Code of Conduct -

How to pass data from one tab to another.

  • Hello Everyone,
    I am learning Qt and stuck on below problem.
    I have a gui which have more than 10 tabs. I have created one new tab. In that new tab I am using QtLineEdit. I want to fill this QtLineEdit with data from another tab's QTableView.
    Any help would be appreciated.

  • Lifetime Qt Champion

    @pavel213 Well, you have access to all your widgets in same parent widget via ui->, so simply access what is needed using ui->NAME_OF_WIDGET_OBJECT.

  • @jsulm Thanks for reply. Would you please elaborate your answer. I don't have much experience in qt.

  • Lifetime Qt Champion

    @pavel213 Do you use QtDesigner to design your UI?
    If you do, then you can access ALL your widgets using ui->:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        ui->lineEdit->setText("Some text");

  • @pavel213
    well, you have several ways to do this, much depends on the type of data ( temporary data, data that your core application uses, ... ).

    As jsulm says you have access to all widgets, so from the parent you can get data from a tab and give them to other tab/tabs.
    MyData data = ui->tab1->getMyData()

    Via signals, when data changes you can emit a signal with the new value/s.
    connect( ui->tab1, SIGNAL( myDataChange( MyData ), ui->tab2, SLOT( newValueData(MyData) ); // For example I use old style connect

    Via data manager, if your data is data useful for your core application.
    In the tab1: CoreDataManager->setMyData( data );
    In the tab2: data = CoreDataManager->getMyData( );

    and so on.

    As I already say, much depends on the type of data and how you use them.

  • Gui is built in VS 2015 and qt 5.9.9.

    Following is the code snippet.
    From this tab "MultitranpondersTab" i want to pick data "a_list".

    void MultiTranspondersTab::cycle()
        QByteArray answer, uid;
    	bool gotAnswer;
    	ST25R3911ComError err;
    	QList<Card*> card_list;
    	gotAnswer = true;
    	err = this->com->antennaEnableRFField(true);
        if (err)
            emit mainWindowDisplayMessage("ext field detected, not polling");
            goto end_scan;
    		int counter = 10;
    		QList<Iso14443aCard> a_list;
            err = com->kovioRead(uid);
            if (ST25R3911ComError::NoError == err)
                if (uid.size() > 0) {
                    // avoid the "kovio situation"
                    Card *card = new Card(uid, QString("Kovio BC"));
                    emit mainWindowDisplayMessage("problem receiving data..");
                goto end_scan; // no sense to continue to other protocols since Kovio barcode will screw all other communications
    			gotAnswer = false;
                int list_size = a_list.size(); /* list_size is a Workaround for early AS3953 versions which did not recognize HLTA command */
                if (itsAnalogState)
    			err = this->com->iso14443aREQA(&a_list, uid);
    			if(err == ST25R3911ComError::NoError && a_list.size()>list_size)

    and following is the tab where i want to use it:

    WiegandTab::WiegandTab(ST25R3911Communication* com, QWidget *parent)
    	: QWidget(parent)
    	, itsCom(com)
    	, itsCurrentCardRow(0)
    void WiegandTab::on_txrx_pb_clicked(bool clicked)
    	/*if (felicaCardsTw->rowCount() < 0)
    	WiegandTab wTabObj;
    	ST25R3911ComError err;
    	QByteArray wiegandData, fc, cid, rx;
    	const QByteArray	tempCard;
    	int w_frmt;
    	bool ok;
    	QString format = this->comboBox->currentText();
    	w_frmt = format.split(" ").last().toInt(&ok, 16);
    #if QT_VERSION < 0x050000
    	fc = QByteArray::fromHex(this->txbytes_le->text().toAscii());
    	cid = QByteArray::fromHex(this->txbytes_le_2->text().toAscii());
    	fc = QByteArray::fromHex(this->txbytes_le->text().toLatin1());
    	cid = QByteArray::fromHex(this->txbytes_le_2->text().toLatin1());
    	//some where here i want to use "a_list" 
            // basically want to put "a_list" data in  "txbytes_le_2"
    	/*if (item && this->idm_box->isChecked())
    #if QT_VERSION < 0x050000
    		idm = QByteArray::fromHex(item->text().toAscii());
    		idm = QByteArray::fromHex(item->text().toLatin1());
    	//qDebug() << "wiegandTxRxNBytes() idm =" << idm.toHex();
    	qDebug() << "wiegandTxRxNBytes() tx =" << wiegandData.toHex();
    	err = itsCom->wiegandTxRxNBytes(wiegandData, rx);
    	if (err == ST25R3911ComError::NoError)
    	emit mainWindowDisplayMessage(QString("wiegandTxRxNBytes[%1]").arg(itsCom->decodeFWError(err)) + wiegandData.toHex() + "->" + rx.toHex());

  • @pavel213
    If I have well understood your code a_list is a local QList (MultiTranspondersTab class), it is declared in the function body. So the only way I see is sending a copy of data to another tab (WiegandTab class) via connect, see the previous post.

  • @pavel213
    Only for example:

    In MultiTranspondersTab.h :

    void sendIsoData(QList<Iso14443aCard>);

    In WiegandTab.h :

    public slots:
    void onNewIsoData(QList<Iso14443aCard>);

    in parent of MultiTranspondersTab.h and onNewIsoData:
    connect( pMultiTranspondersTab, &MultiTranspondersTab::sendIsoData, pWiegandTab, &WiegandTab::onNewIsoData );

    In MultiTranspondersTab.cpp
    somewhere in MultiTranspondersTab::cycle() you have to write emit sendIsoData(a_list);

Log in to reply