[SOLVED] Problem with duplication of Value in QTabWidget
-
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? -
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?
-
-
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. -
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;
}
@ -
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!