Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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.
    Thanks.


  • 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->setupUi(this);
        ui->lineEdit->setText("Some text");
    }
    


  • @pavel213
    Hi,
    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()
    ui->tab2->setMyData(data)

    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;
        }
    
    	onTriggerProgressBar();
    	SleepThread::msleep(10);
    
    	if(this->isoA_cb->isChecked())
    	{
    		int counter = 10;
    		QList<Iso14443aCard> a_list;
    
            com->kovioInitialise();
            err = com->kovioRead(uid);
            com->kovioDeinit(true);
            if (ST25R3911ComError::NoError == err)
            {
                if (uid.size() > 0) {
                    // avoid the "kovio situation"
                    Card *card = new Card(uid, QString("Kovio BC"));
                    card_list.append(card);
                }
                else
                {
                    emit mainWindowDisplayMessage("problem receiving data..");
                }
                goto end_scan; // no sense to continue to other protocols since Kovio barcode will screw all other communications
            }
            this->com->iso14443aInit();
    		onTriggerProgressBar();
    		do 
    		{
    			counter--;
    			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)
                {
                    SleepThread::msleep(delay_sb->value());
                }
    			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)
    {
    	this->setupUi(this);
    	
     
    
        
    	this->txrx_pb->setEnabled(true);
    }
    
    void WiegandTab::on_txrx_pb_clicked(bool clicked)
    {
    	/*if (felicaCardsTw->rowCount() < 0)
    		return;*/
    	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());
    #else
    	fc = QByteArray::fromHex(this->txbytes_le->text().toLatin1());
    	cid = QByteArray::fromHex(this->txbytes_le_2->text().toLatin1());
    #endif
    	
    	//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());
    #else
    		idm = QByteArray::fromHex(item->text().toLatin1());
    #endif
    	}*/
    
    	//qDebug() << "wiegandTxRxNBytes() idm =" << idm.toHex();
    
    	wiegandData.append(w_frmt);
    	wiegandData.append(fc);
    	wiegandData.append(cid);
    
    	qDebug() << "wiegandTxRxNBytes() tx =" << wiegandData.toHex();
    
    	err = itsCom->wiegandTxRxNBytes(wiegandData, rx);
    	if (err == ST25R3911ComError::NoError)
    	{
    		//rxbytes_le->setText(rx.toHex());
    	}
    	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 :

    signals
    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