PDA

View Full Version : customslots in qt4



deekayt
19th October 2006, 18:37
Hello designers

I am using qt4 ( windows)

Howdoes one add custom slot .:confused:
And where to write the code for that custom slot function as we used to do in qt3.
By the way is there qt3 version for windows ( integrated type) as we have for Unix/Linux.
and if it is there where to get it .:crying:

Thanks in advance

jacek
19th October 2006, 19:13
Howdoes one add custom slot .:confused:
And where to write the code for that custom slot function as we used to do in qt3.
In Qt3 there were two ways: hackish .ui.h files and elegant subclassing approach. In Qt4 only the latter is available, so you can't edit code in Qt Designer (since it's a designer, not IDE) and you can't add custom slots to .ui files.

Read this: http://doc.trolltech.com/4.2/designer-using-a-component.html (especially the "The Single Inheritance Approach" section).



By the way is there qt3 version for windows ( integrated type) as we have for Unix/Linux.
I'm not sure what you are asking for.

deekayt
19th October 2006, 20:01
Hmm...
I have gone through other threads of porting from qt3.3.4(Linux)to qt 4(windows) in which you have extensively supported.
I shall try to port the code.
But just a little bit of doubt.
The custom slots which I added in qt3.3.4(linux) was through this hckish.ui.h approach and not by subclassing.( infact the quickstart help of qt3.3.4 ( linux ) gives the example of that "metric conversion" using this hckish.ui.h approach to add custom slots)
NOw when I port the code will it be ok.

please advise
thanks

jacek
19th October 2006, 20:27
infact the quickstart help of qt3.3.4 ( linux ) gives the example of that "metric conversion" using this hckish.ui.h approach to add custom slots
I guess they did that just not to scare away newbies, that are afraid of adding few extra lines of code, even if it would grant them more flexibility in the future.

Actually there is no point in adding custom slots to the .ui file, since your application won't use the code generated from it directly. What it will use is the class you are going to write and that class can have any slots and signals you want and any constructor you want. You can even derive it from some custom base class. This approach gives you full control over your source on the contrary to the .ui.h files.

deekayt
19th October 2006, 20:39
ok fine let me first try and port
any problems faced i will come back with the error messages.
bye

deekayt
20th October 2006, 18:03
• I have gone through the document “Qt 4_2 Using a Component in Your Application”and have been able to run the direct approach. However whatever I try for the The Single Inheritance Approach , The Multiple Inheritance Approach and Automatic Connections , Without and with Auto-Connect the code is not coming right .
• In case of direct approach I just make a dialog with say buttons on the right , put it in a folder with main.cpp file ( as given below) and run qmake and make. The thing works .

#include "ui_imagedialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog *window = new QDialog;
Ui::ImageDialog ui;
ui.setupUi(window);

window->show();
return app.exec();
}

Now when I want to adopt the other approaches where should I put the codes


class ImageDialog : public QDialog
{
Q_OBJECT

public:
ImageDialog(QWidget *parent = 0);

private:
Ui::ImageDialog ui;
};
Should it be put in main above the int main() function.
And what should I now have in main() function.

And for the code given below should I have some other header file.

ImageDialog::ImageDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

ui.colorDepthCombo->addItem(tr("2 colors (1 bit per pixel)"));
ui.colorDepthCombo->addItem(tr("4 colors (2 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("16 colors (4 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("256 colors (8 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("65536 colors (16 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("16 million colors (24 bits per pixel)"));

connect(ui.okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
I have triad all combinations but I am not getting it right in any of the approaches

Please please guide me
I shall be very grateful

deekayt

jacek
20th October 2006, 18:23
what should I now have in main() function.
You main should look more or less like this:

#include <QApplication>
#include "imagedialog.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);
ImageDialog dlg;
dlg.show();
return app.exec();
}


And for the code given below should I have some other header file.
In general you put class definitions in header files (*.h) and method implementation in implementation files (*.cpp).

deekayt
20th October 2006, 18:43
You have included "imagedialog.h" is ti the one generated by the uic .Should it not be ui_imagedialog.h. Or is it that the class definition



class ImageDialog : public QDialog
{
Q_OBJECT

public:
ImageDialog(QWidget *parent = 0);

private:
Ui::ImageDialog ui;
};

is the one which will be put in the imagedialog.h or do I make a new header fileand put the above class definition and below thing in that .I will have to include that in the project file also.


ImageDialog::ImageDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

ui.colorDepthCombo->addItem(tr("2 colors (1 bit per pixel)"));
ui.colorDepthCombo->addItem(tr("4 colors (2 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("16 colors (4 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("256 colors (8 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("65536 colors (16 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("16 million colors (24 bits per pixel)"));

connect(ui.okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}

GUide me please

jacek
20th October 2006, 19:06
You have included "imagedialog.h" is ti the one generated by the uic .Should it not be ui_imagedialog.h.
No, ui_imagedialog.h is the one where Ui::ImageDialog is defined. imagedialog.h (or whatever you will call it) is a header file with ImageDialog definition.

Take a look at QTDIR/examples/designer/calculatorform.


GUide me please
Actually these are complete C++ basics, that you should know before even thinking about using Qt.

deekayt
21st October 2006, 08:48
THANK YOU VERY MUCH
I WAS ABLE TO RUN THE APPLICATION ATLAST ....

I need to first start some processes fromthe mother application ( dialog) on the clickof the pushbutton. I tried the Q process but it didnot work out.Then I resorted to System call . This I know is not a clean process but just to check the slot thing I have put this now.

my code snippet is as

steg::steg(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(test()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(open_file()));
}

void steg::test()
{
system("e:\"\"\"stego.jpg");
}

you see above the okbutton on being clicked gives signal to slot test() .test() function has system call to launch picture viewer to show the file stego.jpg.
Canyou suggest a better method to do this task.

Secondly I need to create , open , write , execute and close the file very often in my program on the click of buttons.

i tried the following snippets


void steg::writeinfile()
{
char eog_comand[]= "eog";
char space[]= " ";
FILE *fp;
fp = fopen ("e:\"\"\"trial.txt", "w+");
fputs (eog_comand,fp);
fputs (space,fp);
fclose(fp);
}
this is simple c code .But when i run the program it crashes.
I tried Qfile commands

void steg::writeinfile()
{
QFile file;
file.setFileName("e:\"\"\"trial.txt");
file.open(QIODevice::WriteOnly);
file.write(msg, qstrlen(msg)); // write to stderr
file.close();
}
But that alsodoesnot work
Someguidance please.

jacek
21st October 2006, 20:37
Thread splitted: http://www.qtcentre.org/forum/f-qt-programming-2/t-re-customslots-in-qt4-4124.html