PDA

View Full Version : Text box and list box connectivity



Rekha
25th July 2006, 06:14
hi,
i'm beginner to Qt.. there is a text box and list box,when u enter some characters in the text box and give enter to a push button,it should add all the characters to listbox.. so i had written code 4 tat,everything works fine,but adding characters to listbox is not wrkg fine.. the code for adding characters is given below,
pls check wats wrong with code

void show()
{
QTextLength str1;
QTextStream str,str2;
str=text1.text();
str1=length(str);

for(int i=0;i<str1;i++)
{

str2=insertText(list1.at(i));
i++;
}
display->setText(display->text()+str2);
}

munna
25th July 2006, 06:28
Please use the code tag to insert your code.


there is a text box and list box,when u enter some characters in the text box and give enter to a push button,it should add all the characters to listbox..

How should the characters be added to the list box ? Should each character become an item in the listbox or each word becomes an item or each line should be an item in the list box ?



void show()
{
QTextLength str1;
QTextStream str,str2;
str=text1.text();
str1=length(str);

for(int i=0;i<str1;i++)
{

str2=insertText(list1.at(i));
i++;
}
display->setText(display->text()+str2);
}


What is list1 and display ?

Rekha
25th July 2006, 06:37
oh sorry,
text1 is the name of text box widget
list1 is the name of the listWidget name
display is the function used to display
the complete text thats there in text box text1 should come to the list box, it can come as a block or else word by word or character by character

munna
25th July 2006, 06:42
try this




void show()
{
list1.addItem(text1.toPlainText());
}

Rekha
25th July 2006, 06:52
no its not wkg..
i will add code what i had written again 4m starting
/////////////////code/////////////////


class Ui_Dialog
{
public:
QWidget *layoutWidget;
QHBoxLayout *hboxLayout;
QPushButton *exit;
QTextEdit *text2;
QListWidget *list1;
QTextEdit *text1;
QPushButton *enter;

void setupUi(QDialog *Dialog)
{
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(QSize(552, 555).expandedTo(Dialog->minimumSizeHint()));
layoutWidget = new QWidget(Dialog);
layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
layoutWidget->setGeometry(QRect(20, 430, 351, 38));
hboxLayout = new QHBoxLayout(layoutWidget);
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
exit = new QPushButton(layoutWidget);
exit->setObjectName(QString::fromUtf8("exit"));

hboxLayout->addWidget(exit);

text2 = new QTextEdit(Dialog);
text2->setObjectName(QString::fromUtf8("text2"));
text2->setGeometry(QRect(60, 220, 104, 52));
list1 = new QListWidget(Dialog);
list1->setObjectName(QString::fromUtf8("list1"));
list1->setGeometry(QRect(380, 90, 120, 80));
text1 = new QTextEdit(Dialog);
text1->setObjectName(QString::fromUtf8("text1"));
text1->setGeometry(QRect(60, 80, 104, 52));
enter = new QPushButton(Dialog);
enter->setObjectName(QString::fromUtf8("enter"));
enter->setGeometry(QRect(210, 90, 108, 36));
QWidget::setTabOrder(text1, enter);
QWidget::setTabOrder(enter, list1);
QWidget::setTabOrder(list1, text2);
QWidget::setTabOrder(text2, exit);
retranslateUi(Dialog);
QObject::connect(exit, SIGNAL(clicked()), Dialog, SLOT(reject()));
QObject::connect(enter, SIGNAL(clicked()), list1, SLOT(show()));
QObject::connect(list1, SIGNAL(itemDoubleClicked(QListWidgetItem*)), text2, SLOT(copy()));

QMetaObject::connectSlotsByName(Dialog);
} // setupUi

void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
exit->setText(QApplication::translate("Dialog", "Exit", 0, QApplication::UnicodeUTF8));
enter->setText(QApplication::translate("Dialog", "Enter", 0, QApplication::UnicodeUTF8));
Q_UNUSED(Dialog);
} // retranslateUi

void show()
{
QTextLength str1;
QTextStream str,str2;
str=text1.text();
str1=length(str);
list1.addItem(text1.toPlainText());
for(int i=0;i<str1;i++)
{

str2=insertText(list1.at(i));

i++;
}
display->setText(display->text()+str2);
}


};


The problem is in void show() ;

munna
25th July 2006, 06:59
change

QObject::connect(enter, SIGNAL(clicked()), list1, SLOT(show()));

to

QObject::connect(enter, SIGNAL(clicked()), Dialog, SLOT(show()));

Rekha
25th July 2006, 07:07
its not working :(

munna
25th July 2006, 07:37
QObject::connect(enter, SIGNAL(clicked()), Dialog, SLOT(show()));

sender - enter
signal - clicked()
receiver - Dialog
slot - show()

receiver tells the program where the slot is. Therefore the slot show() should be in the class Dialog and not in the Ui_Dialog.

Sorry, my mistake. I did not see the slot in that big code.

Please insert you code between the code tag.

Rekha
25th July 2006, 07:46
at the end of code i had used namespace so its" Dialog class" only..

munna
25th July 2006, 07:52
In that case it should be

QObject::connect(enter, SIGNAL(clicked()), this, SLOT(show()));

and proabably u'll need to add "public slots:" before the slot show(), if you have not added.

Rekha
25th July 2006, 09:08
sorry to say but even that dint work.. can u guess what might be the problem:(

munna
25th July 2006, 09:58
Can you please zip your code and attach here ?

jacek
25th July 2006, 10:21
Ui_Dialog class doesn't inherit QObject, thus it can't have slots.

It looks like you are trying to modify a file produced by uic. You should use the single- or multi-inheritance approach instead, to avoid problems when you will have to generate it again. See Qt Designer docs (http://doc.trolltech.com/4.1/designer-using-a-component.html#the-single-inheritance-approach).

Rekha
25th July 2006, 10:25
i have zipped the sample.h file as u had asked..i have attached sample.cpp also along with that

munna
25th July 2006, 10:38
ok here it is




class Ui_Dialog : public QObject
{
Q_OBJECT

public:
QWidget *layoutWidget;
QHBoxLayout *hboxLayout;
QPushButton *exit;
QTextEdit *text2;
QListWidget *list1;
QPushButton *enter;
QTextEdit *text1;

void setupUi(QDialog *Dialog)
{
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(QSize(552, 555).expandedTo(Dialog->minimumSizeHint()));
layoutWidget = new QWidget(Dialog);
layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
layoutWidget->setGeometry(QRect(230, 410, 131, 38));
hboxLayout = new QHBoxLayout(layoutWidget);
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
exit = new QPushButton(layoutWidget);
exit->setObjectName(QString::fromUtf8("exit"));

hboxLayout->addWidget(exit);

text2 = new QTextEdit(Dialog);
text2->setObjectName(QString::fromUtf8("text2"));
text2->setGeometry(QRect(60, 220, 104, 52));
list1 = new QListWidget(Dialog);
list1->setObjectName(QString::fromUtf8("list1"));
list1->setGeometry(QRect(380, 90, 120, 80));
enter = new QPushButton(Dialog);
enter->setObjectName(QString::fromUtf8("enter"));
enter->setGeometry(QRect(210, 90, 108, 36));
text1 = new QTextEdit(Dialog);
text1->setObjectName(QString::fromUtf8("text1"));
text1->setGeometry(QRect(60, 80, 104, 52));
QWidget::setTabOrder(text1, enter);
QWidget::setTabOrder(enter, list1);
QWidget::setTabOrder(list1, text2);
QWidget::setTabOrder(text2, exit);
retranslateUi(Dialog);
QObject::connect(exit, SIGNAL(clicked()), this, SLOT(reject()));
QObject::connect(enter, SIGNAL(clicked()), this, SLOT(show()));
QObject::connect(list1, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(copy()));
QObject::connect(text1, SIGNAL(textChanged()), this, SLOT(update()));

QMetaObject::connectSlotsByName(Dialog);
} // setupUi

void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
exit->setText(QApplication::translate("Dialog", "Exit", 0, QApplication::UnicodeUTF8));
enter->setText(QApplication::translate("Dialog", "Enter", 0, QApplication::UnicodeUTF8));
Q_UNUSED(Dialog);
} // retranslateUi

public slots:

void show()
{
list1->addItem(text1->toPlainText());
}


};



But I would not suggest you to do this way. Please look into Qt examples as on how the classes are designed.

Rekha
25th July 2006, 12:32
its not working .. please say me
1)the code to copy all the characters from the text box to list box when a push button like enter is being clicked after entering all the characters in the text box....... is not working:o

munna
25th July 2006, 12:39
I have tested this and it works perfectly for me. When I click on the enter button the text gets copied to the list1.

Ofcourse u'll need to add

#include<QObject> in the beginning

and

namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui


at the end

Rekha
28th July 2006, 10:08
on executing "make" command i'm getting the follwoing errors..
wat 2 do?



MyDialog.cpp: In member function `void MyDialog::copy()':
MyDialog.cpp:16: error: `addItem' undeclared (first use this function)
MyDialog.cpp:16: error: (Each undeclared identifier is reported only once for
each function it appears in.)
make: *** [MyDialog.o] Error 1

sunil.thaha
28th July 2006, 12:46
Just go through this
http://qt4.digitalfanatics.org/articles/i.html