Important: Please read the Qt Code of Conduct -

How to identify wich tab has been opened & receive data from serial port [SOLVED]

  • Hi everyone,

    I am trying to create a tool which will receive data by serial port and send to different Tabs (Tab Widget). Does anyone know how I can send the data to its respective tab? Moreover, I would like to know how to identify which tab has been selected by the user. I have tried to use @int NumTab = ui->tabWidget->currentIndex() @ but the index number changes randomly from tab to tab from time to time.
    I will appreciate any tip with code...

  • Lifetime Qt Champion

    Im not sure what you mean by sending data to the tab since a tab is a container so I assume you have a text widget you will send the data or can you explain what you want?

    Normally tabWidget->currentIndex() will return 0 for first tab, 1 for next so Im not sure how it can be random for you?
    qDebug() << ui->tabWidget->currentIndex();
    give you random numbers too?

  • Hi @mrjj ,

    1- I want to display the data coming through the serial in 3 different tab (Data, Application, Config). Every time the user click in one of the tabs a command will be sent to my device (in this case it will write on the serial port) and, consequently, data will come. This data needs to be displayed on its correspondent tab.
    Let suppose that a user have selected the Application tab, then it needs to send (write) "ap" to the device. Once it is done, the device will send some info, such as application type, year, etc, which will be displayed in Line Edit on the Application tab.

    2- What I meant with the random number is that it changes from tab to tab. Let say that one select the following sequence Data -> Application -> Config -> Application, it will have the index: 2 -> 0 -> 1 -> 2. Note that the index from the Application tab changed from 0 to 2. The idea was to use the index to identify which tab has been opened and therefore send the correct command to the device and receive the data from it.

    I hope you can help me with that.... :)
    Thank you

  • Lifetime Qt Champion

    Ok I understand. Good Description.

    It sounds very strange with index.
    Just to be sure I understand.
    Data -> Application -> Config -> Application,
    Clicking on them and Then when clicking on Application again,
    It no longer has index 0 but 2 ?

    Do you take the index directly with
    ui->tabWidget->currentIndex() or the
    index from signal void currentChanged(int index) ?

    Those tabs are inserted at startup ? you do not add/remove them while program runs?

  • Hi @mrjj

    1 - Yes, You understood right!

    2 - I have implemented on the following way:
    void MainWindow::on_tabWidget_tabBarClicked(int index)
    int checkindex = ui->tabWidget->currentIndex();
    qDebug()<< checkindex;

    3- Yes, the tabs are inserted at startup. They were implemented with Design forms from Qt (into the main window). Moreover, they will not be add/remove while program runs.

  • Lifetime Qt Champion

    @rvrv said:

    Hi it seems that the OnClick is not the way to go as
    sometimes it reports the other tabs index as it has not really changed yet.
    First you click, it sends on click and then it changes index.

    So its better not to use ON click but tel it to call us once it really have changed tab
    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(on_tabWidget_tabBarClicked()));

    Put it after
    so it override the Onclick connection.
    and in the void MainWindow::on_tabWidget_tabBarClicked(int index)
    you use the Index that it provides.
    Then Index will be the same all the time and no funny stuff.

    My test sample

  • Hi @mrjj ,

    It worked.

    Tks a lot.

  • Lifetime Qt Champion

    Remember to close question if possible :)

Log in to reply