PDA

View Full Version : Solving some errors.



srohit24
8th April 2009, 10:01
I am using QtCreator. I am getting this bizaare linker error when i try to combine this app.

This is what my app is.

I have made some changes since my last post. This is my code at the moment.

I need a GUI application to display the contents of myFunction. I have a working console application.

This is my dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QLibrary>
#include <QtGui>

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{

disLabel = new QLabel;

pushButton = new QPushButton(tr("Sync"));

connect(pushButton, SIGNAL(clicked()), this, SLOT(myFunction1()));

setWindowTitle(tr("Desktop Uploader"));
}


void Dialog::myFunction1()
{
QLibrary myLib("dllprog");
typedef char* (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) QLibrary::resolve("dllprog", "TestDll");
char *b = myFunction();
disLabel->setText(tr(b));

}

This is my dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QLabel;
class QPushButton;

class FindDialog : public QDialog
{
Q_OBJECT

public:
FindDialog(QWidget *parent = 0);

private:
QLabel *disLabel
QPushButton *pushButton;
QWidget *extension;
};

#endif // DIALOG_H

This is my main.cpp


#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog dialog;
return dialog.exec();
}


I am getting the foll errors.


main.cpp:7: error: `Dialog' was not declared in this scope
/main.cpp:7: error: expected `;' before "dialog"
main.cpp:8: error: `dialog' was not declared in this scope
/main.cpp:7: warning: unused variable 'Dialog'
/main.cpp:8: warning: unused variable 'dialog'


How can i solve this?

thanks for reading.

faldzip
8th April 2009, 10:21
what error did you get?

srohit24
8th April 2009, 10:40
i have added the errors.

spirit
8th April 2009, 10:54
you calss is called FindDialog not Dialog according to this


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QLabel;
class QPushButton;

class FindDialog : public QDialog
{
Q_OBJECT

public:
FindDialog(QWidget *parent = 0);

private:
QLabel *disLabel
QPushButton *pushButton;
QWidget *extension;
};

#endif // DIALOG_H

jjbabu
8th April 2009, 10:54
u r defined class with the name "FindDialog".and u r creating object for "Dialog".
u have not defined class with the name "Dialog",check ur code properly.

jjbabu
8th April 2009, 10:57
in class "FindDialog" also u have not declared ur slot name called "myFunction1()",it will also create error later.verify total code once and then compile

srohit24
8th April 2009, 11:11
sorry, bad silly mistake. fixed it :o

thanks guys fixed the errors.

I am getting a window, in which there is no label or pushbutton that i am trying to use.

I have put a label and pushbutton, and have named them according in the .ui file.

But when i run the app i get a blank window.

where have i gone wrong??

spirit
8th April 2009, 11:13
I don't see setupUi(this) in you ctor and I also don't see any Ui::* declaration.

srohit24
8th April 2009, 11:20
ok. i have added that as well.


#include "dialog.h"
#include "ui_dialog.h"
#include <QLibrary>
#include <QtGui>




Dialog::Dialog(QWidget *parent)
: QDialog(parent) ,ui(new Ui::DialogClass)
{

ui->setupUi(this);
disLabel = new QLabel;

pushButton = new QPushButton(tr("Sync"));
pushButton->setDefault(true);

connect(pushButton, SIGNAL(clicked()), this, SLOT(myFunction1()));

setWindowTitle(tr("Desktop Uploader"));
}


void Dialog::myFunction1()
{
QLibrary myLib("dllprog");
typedef char* (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) QLibrary::resolve("dllprog", "TestDll");
char *b = myFunction();
disLabel->setText(tr(b));
}


Now i am nt able to make the connection.
When i press the push button, the text is not being written in the label

spirit
8th April 2009, 11:25
read post #6.
add this to h-file


...
public slots:
void myFunction1();
...

and also I don't see where you delete ui. :)

srohit24
8th April 2009, 11:33
I have made all the changes you told me to, still the prob exists.

No link between pushButton and label.


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QLabel;
class QPushButton;

namespace Ui
{
class DialogClass;
}
class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);
~Dialog();

public slots:
void myFunction1();

private:
QLabel *disLabel;
QPushButton *pushButton;
Ui::DialogClass *ui;
};

#endif // DIALOG_H



#include "dialog.h"
#include "ui_dialog.h"
#include <QLibrary>
#include <QtGui>




Dialog::Dialog(QWidget *parent)
: QDialog(parent) ,ui(new Ui::DialogClass)
{

ui->setupUi(this);
disLabel = new QLabel;

pushButton = new QPushButton(tr("Sync"));
pushButton->setDefault(true);

connect(pushButton, SIGNAL(clicked()), this, SLOT(myFunction1()));

setWindowTitle(tr("Desktop Uploader"));
}


void Dialog::myFunction1()
{
QLibrary myLib("dllprog");
typedef char* (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) QLibrary::resolve("dllprog", "TestDll");
char *b = myFunction();
disLabel->setText(tr(b));
}

Dialog::~Dialog()
{
delete ui;
}




#include <QtGui/QApplication>
#include "dialog.h"

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

spirit
8th April 2009, 11:36
I would change this code


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

to that


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

make next things: make clean & qmake & make.

srohit24
8th April 2009, 11:51
thanks mate.

Can you explain y i shoulf use app.exec();
rather than dialog.exec?

srohit24
8th April 2009, 12:02
connect(pushButton, SIGNAL(clicked()), this, SLOT(myFunction1()));

disLabel->setText(tr("b"));

I am returning a char b in the myFunction1() to check is the connection is working. When irun the app, same prob. no linking.

where is the mistake?

spirit
8th April 2009, 12:30
thanks mate.

Can you explain y i shoulf use app.exec();
rather than dialog.exec?

when you use app.exec that means that application event loop is started, but in you code when you use dialog.exec means that a dialog event loop is started.

so, if you want to create an another widget in you dialog and if you close your dialog (after closing event loop will be terminated) that widget which you created will be closed too, but if you use app.exec() that doesn't happen, becuse you don't terminate event loop.

spirit
8th April 2009, 12:31
what kind of error do you get?

srohit24
8th April 2009, 16:38
what kind of error do you get?

I am not getting any errors.

The app is not able to link the button and label in the designer with the actual code in the dialog.cpp.


QGridLayout *layout = new QGridLayout;
layout->setColumnStretch(1, 1);
layout->setColumnMinimumWidth(1, 1);
layout->addWidget(syncButton, 0, 0);
layout->addWidget(disLabel, 0, 1);
setLayout(layout);

setWindowTitle(tr("Desktop Uploader"));
}

I had to write this code explicitly so that the label and the button are linked and it shows the desired result.

how can I solve that?

spirit
8th April 2009, 18:08
why you don't add these controls (a button and a label) in ui-file in Qt Disigner?

srohit24
9th April 2009, 06:48
I had, but there was no connection between them

How do i link them to the code?

spirit
9th April 2009, 06:53
to all controls you can get access using ui->.
so, you code should look like


...
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(myFunction1()));
...

that's all.
now you can remove a lable and a pushbutton from class declaration.

srohit24
9th April 2009, 07:30
thanks a lot mate. it fixed my problem.

:D

srohit24
10th April 2009, 05:32
How can i get the input from the users??

i have used this code


void Dialog::gettext()
{
QString text = QInputDialog::getText(this, tr("Enter the username"),
tr("User name:"), QLineEdit::Normal);
ui->dislabel->setText(text);
}

but this code pops up a dialog which i dont need.

What i want is, when i add the text in the lineedit, it should be reflected in the label, or

when i add the text in the lineedit, a variable names text has to store it.

spirit
10th April 2009, 06:15
so, you need to add QLineEdit to your form and then


...
connect(ui->lineEdit, SIGNAL(textEdited(const QString &)), ui->label, SLOT(setText(const QString &)));
...

this code will add a text from lineEdit to label.



...
connect(ui->pushButton, SIGNAL(clicked()), SLOT(getLineEditText()));//get text by clicking on a button
connect(ui->lineEdit, SIGNAL(editingFinished()), SLOT(getLineEditText()));//get text by pressing enter in a lineEdit
//you can also use QLineEdit::returnPressed insted of QLineEdit::editingFinished
...

void MyWidget::getLineEditText()
{
qDebug() << ui->lineEdit->text();
}

srohit24
10th April 2009, 06:59
so, you need to add QLineEdit to your form and then


...
connect(ui->lineEdit, SIGNAL(textEdited(const QString &)), ui->label, SLOT(setText(const QString &)));
...




thanks fore replying :)

in the above code, say, if i want to store the text in a variable, how can i do it?

spirit
10th April 2009, 07:01
use second variant


...
connect(ui->pushButton, SIGNAL(clicked()), SLOT(getLineEditText()));//get text by clicking on a button
connect(ui->lineEdit, SIGNAL(editingFinished()), SLOT(getLineEditText()));//get text by pressing enter in a lineEdit
//you can also use QLineEdit::returnPressed insted of QLineEdit::editingFinished
...

void MyWidget::getLineEditText()
{
const QString myVar = ui->lineEdit->text();
ui->label->setText(myVar);
}

:)

srohit24
10th April 2009, 07:07
thanks mate. you have helped me alot. :cool:

srohit24
10th April 2009, 08:01
mate if i want o add a icon into the application, how can it be done?

I have seen .qrc files. I need to store the image in this particular file

The icon i am talking about is the one is that tiny icon that appears at the top left corner of an application.

spirit
10th April 2009, 08:08
read this (http://doc.trolltech.com/4.5/appicon.html). ;)

faldzip
10th April 2009, 11:39
I think that you should take closer look into Qt Exapmples & Demos and Qt Assistant. There are a lot of exapmples and explanations to all problems you posted here, and it's quite easy find answers there.