Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problem with duplication of Value in QTabWidget
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with duplication of Value in QTabWidget

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M_31
    wrote on last edited by
    #1

    Hi All,
    I have created my QTabWidget by adding QDialog on each Tab's of my QTabWidget..

    and the corresponding QDialog has one QListWidget .

    @
    QTabWidget's Tab1 = QDialog(QListWidget)
    QTabWidget's Tab2 = QDialog(QListWidget)
    @

    @

    // in TempDlg.cpp
    CTempDlg::CTempDlg(QString str)
    {
    ui->listWidget->item(0)->setText(str);
    }

    void CTempDlg::sendData(QString &str)
    {
    str = ui->listWidget->item( 0 )->text();
    qDebug()<<"the list Widget text is "<<str;
    }

    //in TestDlg.h
    #include "TempDlg.h"
    class CTestDlg : public QDialog
    {
    public:
    CTempDlg *tempDlg;
    }

    // in TestDlg.cpp

    void CTestDlg::addTab()
    {

    for(int i =1; i<= 2 ; i++ )
    {
    QString str = QString::number(i);

        tempDlg = new CTempDlg(str)
    
       connect( this, SIGNAL(getData(QString&)), tempDlg, SLOT(sendData(QString&)));
       ui->tabWidget->addTab( tempDlg , QString("TAB").append(QString::number(i)) );}
    

    }

    void CTestDlg::printData()
    {
    // I am doing this print from First & Second tab of my QTabWidget
    QString data;
    emit getData( data);
    qDebug()<<"the Data value is "<<data;
    }
    @
    //Here i am getting always the ouput as :

    the list Widget text is "1"
    the list Widget text is "2"
    the Data value is "2"

    //Both from First Tab & second Tab
    //please let me know whether i am doing anything wrong on this?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Dialogs have no place on tabs. QDialog is a class that is designed to be used as a toplevel window, not as a widget in another form.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        M_31
        wrote on last edited by
        #3

        Thanks for you reply...

        You mean to say ..
        I have to add those QListWidget control directly to QTabWidget....
        Instead of adding into QDialog and then to QTabWidget?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Yes. QDialog is mend to create a separate window (with window decoration and everything that belongs to it), not for use on another widget.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            M_31
            wrote on last edited by
            #5

            Hi Andre...
            Now i have created derived class for QListWidget and added to QTabWidget's tab ..
            But Still getting the same error..

            @
            //in CustomListWidget.cpp

            #include "CustomListWidget.h"
            #include <QDebug>

            CustomListWidget::CustomListWidget(QString str , QWidget *parent) :
            m_str(str),
            QListWidget(parent)
            {
            listItem = new QListWidgetItem(m_str);
            addItem( listItem );
            }
            void CustomListWidget::sendData(QString &str)
            {
            str = item( 0 )->text();
            qDebug()<<"the list Widget text is "<<str;
            }

            // in CTestDlg.cpp
            CTestDlg::CTestDlg(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::CTestDlg)
            {

            ui->setupUi(this);
            

            for(int i =1; i<= 2 ; i++ )
            {
            customeListWidget = new CustomListWidget(QString::number(i) );
            connect( this , SIGNAL(getData(QString&)), customeListWidget, SLOT(sendData(QString&)));
            ui->tabWidget->addTab( customeListWidget, QString("TAB").append(QString::number(i) ) );
            }
            }

            void CTestDlg::on_pushButton_clicked()
            {
            QString data;
            emit getData( data);
            qDebug()<<"the Data value is "<<data;
            }
            @

            this is the OUTPUT for both TABs always

            the list Widget text is "1"
            the list Widget text is"2"
            the Data value is "2"

            Whether i am missing anything here?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              M_31
              wrote on last edited by
              #6

              Andre...This may be the bug with QTabWidget??

              or i have to use QStackWidget explicitly instead of QtabWidget tab page??? to achive my task..

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                Please stop bumping a topic after just 90 minutes. We're not waiting on front of the screen for someones replies - this is a forum, not a chat! Please be more patiently, otherwise your risk being ignored and not getting back an answer at all. Thanks.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Indeed, if you want guaranteed-time responses to your support queries, either pay me or someone else who supplies that for such a service. Other than that: I sometimes just feel like not being bussy with Qt and computers in general.

                  Your code in general makes little sense. What is line 38 above supposed to do? Why do you emit a signal to get data, and what slot is connected to it?

                  [quote author="M_31" date="1318527110"]Andre...This may be the bug with QTabWidget??

                  or i have to use QStackWidget explicitly instead of QtabWidget tab page??? to achive my task..[/quote]
                  It is generally save to assume that any issue you experience, is an issue with your code, not the code of the well-tested library you are using and that has been used by thousands of other programmers before you to write real-life, working applications. Frankly, I find it a bit insulting that you assume the fault is with somebody else even before you seem to understand the basics of using Qt.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    M_31
                    wrote on last edited by
                    #9

                    I am extremly sorry for my comments.
                    I will not repeat the same ever..

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      M_31
                      wrote on last edited by
                      #10

                      Its working fine now.

                      instead of using SIGNAL/SLOT ..
                      I just driectly type casted and called the corresponding function
                      @

                      // in CTestDlg.cpp
                      CTestDlg::CTestDlg(QWidget *parent) : QDialog(parent), ui(new Ui::CTestDlg)
                      {
                      ui->setupUi(this);

                      for(int i =1; i<= 2 ; i++ )
                      {
                      customeListWidget = new CustomListWidget(QString::number(i) );
                      // connect( this , SIGNAL(getData(QString&)), customeListWidget, SLOT(sendData (QString&)));
                      ui->tabWidget->addTab( customeListWidget, QString("TAB").append(QString::number(i) ) );
                      }

                      }

                      void CTestDlg::on_pushButton_clicked()
                      {
                      CustomListWidget * customeListWidget = ( CustomListWidget *)ui->tabWidget->currentWidget();
                      QString data;
                      data = customeListWidget->getData( );
                      qDebug()<<"the Data value is "<<data;
                      }
                      @

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        C-style casts in C++ are always a bad idea (one of the rare cases where always is an appropriate attribute in development).

                        Instead use dynamic_cast, or even better in case of QObject derived classes a qobject_cast. So please do yourself a favor an change the line to:

                        @
                        CustomListWidget *customeListWidget = qobject_cast<CustomListWidget *>(ui->tabWidget->currentWidget());
                        if(!customeListWidget)
                        return;
                        @

                        And never forget to check if you get back a valid pointer after a cast!

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          M_31
                          wrote on last edited by
                          #12

                          Thanks Volker for your valuable inputs..

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved