PDA

View Full Version : how to get user inputted info into my code?



kja
12th November 2010, 18:43
How can I get user inputted information into my code?

I am making a gui with a qwtplot and I want the user to be able to fill in a text box with a file path. When they push enter I want that path to be transfered into my code so that I can load and plot it. I don't even really know where to start, I have been looking through the qt examples but I can't find one that I can work with.

Do I need to make a custom signal/slot function? If so could anyone point me to an example or show me some code syntax on how it would work?

thanks a lot!

tbscope
12th November 2010, 18:54
Try to get familiar with the components you want to use.
Read their documentation carefully.

http://doc.qt.nokia.com/4.7/qlineedit.html#returnPressed

kja
13th November 2010, 00:28
Thanks for the quick reply.

So I have been reading everything I can find but I'm still missing something.

I have some working code but I'm still unsure how I can get the user inputted text into a variable so that I can load the file.
this is what I have so far:


#include <QtGui>
#include <QApplication>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include "qobjectdefs.h"

class myCanvas : public QWidget{
Q_OBJECT
public:
myCanvas (QWidget * parent = 0);
public Q_SLOTS:
void copyText(const QString & text);// maybe I need to create a slot that copies the text....
};

myCanvas::myCanvas(QWidget *parent):QWidget(parent){
QLineEdit * lineedit = new QLineEdit;
QPushButton * pushbutton = new QPushButton("Enter");

connect(pushbutton, SIGNAL(clicked()), lineedit, SLOT(copyText()));

QVBoxLayout * layout = new QVBoxLayout;
layout->addWidget(lineedit);
layout->addWidget(pushbutton);
setLayout(layout);
}

void myCanvas::copyText (const QString & text)
{
////I don't know....

}

int main (int argc, char * argv[])
{
QApplication app (argc, argv);
myCanvas bob;
bob.show();
return app.exec();
}



Thanks for the help

tbscope
13th November 2010, 06:59
You can't connect signals and slots where the slot has more arguments than the signal.
This would mean that some data needs to come from thin air.

This means that the following is not possible:

connect(pushbutton, SIGNAL(clicked()), lineedit, SLOT(copyText()));

Instead, create a slot like this:


public slots:
void slotButtonClicked();

This slots has maximum the same amount and the same type of arguments as the signal. In this case the clicked() signal of the button.
To connect them, write:


connect(pushbutton, SIGNAL(clicked()), this, SLOT(slotButtonClicked());

Make sure that at least the QLineEdit is available in the whole myCanvas class.
But a general rule is to manage all your pointers, otherwise you might get into trouble.

Thus in your class definition do this:

private:
QLineEdit* lineedit;
QPushButton* pushbutton;

In the implementation you'll get something like:


myCanvas::myCanvas(QWidget* parent) : QWidget(parent) // You might also want to pass the flags to the base class.
{
... // other code maybe

lineedit = new QLineEdit(this);
pushbutton = new QPushButton("Enter", this);

... // other code maybe, like the connection of the signal and slot, the layout, ...
}

Now that the signal/slot connection is right and the widgets are available in the whole class, you can implement the slot:


void myCanvas::slotButtonClicked()
{
QString theText = lineedit->text();
}

You use the line edit inside the slot connected to the button clicked signal.

kja
13th November 2010, 17:51
Thanks a lot, I think I'm now beginning to understand how these work.
Unfortionatly I think I have another problem with how my program is running. I am getting three errors that have something to do with Q_OBJECT


#ifndef SLOTS_H
#define SLOTS_H

#include <QtGui>
#include <QApplication>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>

class myCanvas : public QWidget{
Q_OBJECT
QLineEdit *lineedit;
QPushButton *pushbutton;
public:
myCanvas (QWidget * parent = 0);
public Q_SLOTS:
void slotButtonClicked();
};
#endif SLOTS_H



#include "slots.h"

myCanvas::myCanvas(QWidget *parent):QWidget(parent){
lineedit = new QLineEdit(this);
pushbutton = new QPushButton("Enter", this);
connect(pushbutton, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
QVBoxLayout * layout = new QVBoxLayout;
layout->addWidget(lineedit);
layout->addWidget(pushbutton);
setLayout(layout);
}
void myCanvas::slotButtonClicked()
{
QString theText = lineedit->text();
}
int main (int argc, char * argv[])
{
QApplication app (argc, argv);
myCanvas bob;
bob.show();
return app.exec();
}

errors :

1>Compiling...
1>connectWorking.cpp
1>Linking...
1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall myCanvas::metaObject(void)const " (?metaObject@myCanvas@@UBEPBUQMetaObject@@XZ)
1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall myCanvas::qt_metacast(char const *)" (?qt_metacast@myCanvas@@UAEPAXPBD@Z)
1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall myCanvas::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@myCanvas@@UAEHW4Call@QMetaObject@@HP APAX@Z)
1>\funcLoadData.exe : fatal error LNK1120: 3 unresolved externals
1>Build log was saved at "file://c:...\funcLoadData\Debug\BuildLog.htm"
1>funcLoadData - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

If i comment out Q_OBJECT the program runs but the connection doesn't work

"Object::connect: No such slot QWidget::slotButtonClicked() in c:\.....connectworking.cpp:114"

ChrisW67
13th November 2010, 22:40
Re-run qmake then rebuild the project.

kja
16th November 2010, 00:26
Ahh finally, it works!


The problem was that my .moc file was not being generated because I didn't have the correct properties for my .h file.

All I needed to do was edit the command line and additional dependencies, then add the moc file to my project. Ahem, like I said, I'm pretty new to Qt.


Thanks for all your help tbscope!