LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Qt noob needs help in making simple Qt application (https://www.linuxquestions.org/questions/programming-9/qt-noob-needs-help-in-making-simple-qt-application-771219/)

beli0135 11-24-2009 08:21 AM

Qt noob needs help in making simple Qt application
 
I wish to write a small image resizing application (front-end to imageMagic's 'convert'), but I am total noob in Qt and linux dev tools. Not total noob though (senior Delphi and Adobe Flex/AS3 developer). I have basic knowledge of C++ and Python.

So I took Qt4 designer and designed my form, placed signals and slots, saved file, ... and that is about where my knowledge ends.
I tried to use KDevelop, but it is complex and create bunch of files, and I couldn't find where to import my UI or how to do it. Then i opened QDevelop which is simpler, could link my UI, but gave me bunch of errors as classnames was not the same as template. Tried to do something in it, but with no success.

So, my question is: is there anyone who can teach me how to make an application from an UI form? Since the application is fairly simple, I think it will not take too much time.

Guitarist88 11-24-2009 10:18 AM

There are a lot of great books about using the Qt GUI toolkit. Coming on here asking for someone to do the work for you or tell you how to do everything, essentially, sounds like you have no initiative. Get one of the books... Also, Qt's own documentation is very helpful. There are many examples and tuturials you can work through.

http://doc.trolltech.com/4.3/tutorial.html

DiBosco 11-24-2009 10:38 AM

Can I suggest you use qt-creator?

KDevelop doesn't work on KDE4 and you really need to be developing KDE4 apps now.

Get the IDE from here:

http://qt.nokia.com/products/developer-tools

It has some very nice example projects.

It sounds like you've worked out how to place parts, the next thing, that is hard to get your head round at first, is signals and slots.

As a very simple example if you have placed a button and a label on your form, you can get the button to affect the label in the following way:

Code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->rPushButton,SIGNAL(clicked()),this,SLOT(buttonPressed()));

}

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


void MainWindow::on_rPushButton_clicked()
{

    ui->label->setText("Yo!");
   


}


Here I have used the qizard to create a new application and added a ui form called mainwindow. I called the pushbutton "rPushButton" and the label, er, "label".

The signal is clicked() and it's connecting to the slot on_rPushButton_clicked(). Hopefully that aprt is fairly intuitive.

My mainwindow.h file looks like this:

Code:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>


QT_BEGIN_NAMESPACE
class QPushButton;


namespace Ui
{
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
        QPushButton *ui_findButton;

private slots:
    void on_rPushButton_clicked();


};

#endif // MAINWINDOW_H

Most of it is done automatically by the IDE.

I really do like this new qt-creator. It's fast, not some horrible Java thing like Eclipse and looks really nice.

Good luck and come back and ask if you want more help. :) I found this hideously complex when I first had a go and would never have made it without help from a friend. I don't think you have no initiative if you've got as far as you have.

pixellany 11-24-2009 10:56 AM

For a **simple** front-end, why not dialog ( or kdialog, and other variants) or zenity?

Good book including how to write GUIs: http://www.amazon.com/Beginning-Linu.../dp/1874416680

beli0135 11-25-2009 04:19 AM

Thank you all.
I was lost. Read few tutorials but they dont explain what I need.. just supposing that you know all the tools and "just do it'.
I will look into it. It is all confusing in the beginning.
@Guitarist88 And no, I didn't ask to be done instead of me (I hate that), just to give me guidelines to start. I have read bunch of tutorials and nowhere to find : "you made your UI, now start working with it by opening "THIS_TOOL and do THIS and THAT"

beli0135 11-25-2009 04:57 AM

@DiBosco, qt-creator is so awesome!!!
I think I will manage to do something in it.

Thank you!

beli0135 11-25-2009 08:06 AM

@DiBosco

Yes yes yes! I figured it out, thanks to your example.

DiBosco 11-25-2009 08:09 AM

Quote:

Originally Posted by beli0135 (Post 3769205)
@DiBosco

Yes yes yes! I figured it out, thanks to your example.

Excellent. :)

This is a useful page:

http://doc.trolltech.com/4.3/classes.html

beli0135 11-26-2009 06:20 AM

Believe it or not, my program is almost ready... basic version :)
I will ask you for some things, if I may?

DiBosco 11-26-2009 06:54 AM

Quote:

Originally Posted by beli0135 (Post 3770257)
Believe it or not, my program is almost ready... basic version :)
I will ask you for some things, if I may?

Sure!

beli0135 11-27-2009 04:12 AM

I'd like to keep some settings in text file.
For example if I have:
#
SOURCE=/home/myuser/pictures
DESTINATION=/home/myuser/pictures/resized

How do I read and write it easily? In Lazarus/Delphi, StringList has property "Values", so I can do like this:
MyStringList.values['SOURCE'] = whatever_to_Write
whatever_to_read = MyStringList.values['SOURCE'];

DiBosco 11-27-2009 04:50 AM

Here's an example of (I hope) what you're asking to do:

Code:

void ordernumber::set_initials(QString s)
{

        bool i;

        QString                PathName(QDir::homeDirPath ());
        QString                FullPath;               

        FullPath = (PathName + "/.kde/ordernumber");
        QDir  d1(FullPath);
        if (!d1.exists() )
                {
                d1.mkdir(FullPath);
                }

        QFile f1(FullPath+"/profile.ini");
// Create a DataStream object

        i = f1.open( IO_WriteOnly );
        QTextStream                SaveStream( &f1 );
        SaveStream << s;
        f1.close();


       
}

Hopefully that's easy enough to follow. Making the path to where I want the file first, then setting up the file name, then creating the file, opening it, writing this "stream" to it and finally closing the file.

Bear in mind this is Qt3, so it might be slightly different now. Checkout QTextStream in that link I posted above for more details on what it can do. It's very cool for opening files and quickly parsing them a line at a time. Although in some ways Qt is harder to get to grips with than Delphi at first, I think it's probably more powerful once you get into it.

This snippet from a program I wrote to look through dynamically create files when a USB device is plugged in might give you a bit more of an idea. :~)

Code:

if ((hpVariables[Offset].VarType=="enum") || (hpVariables[Offset].VarType=="bitmask"))
                {
                if (hpVariables[Offset].VarType=="enum")
                        FileName = TypeDirectory + "/enum_val";
                else if (hpVariables[Offset].VarType=="bitmask")
                        FileName = TypeDirectory + "/bit_val";
                QFile f2(FileName);
                i = f2.open( IO_ReadOnly );
                QTextStream        OpenStream( &f2 );
                j=0;
                while (!f2.atEnd())
                        {                       
                        s=OpenStream.readLine();
                        hpVariables[Offset].ParamNames[j] = s;
                        ++j;
                        }
                hpVariables[Offset].ParamQuantity = j;
                f2.close();


beli0135 11-28-2009 10:26 AM

O thanks!

Seems that Qt stores variables just as delphi does.
I will use your example as a basis for my settings read-write.

beli0135 11-28-2009 02:06 PM

Actually, I find class QSettings which is a real nice way to store your settings. Actually you dont think of anything, and let hard part to Qt...

Usage:

Code:

void MainWindow::writeSettings()
 {
    QSettings settings("Moose Soft", "Clipper");

    settings.beginGroup("MainWindow");
    settings.setValue("size", size());
    settings.setValue("pos", pos());
    settings.endGroup();
 }

 void MainWindow::readSettings()
 {
    QSettings settings("Moose Soft", "Clipper");

    settings.beginGroup("MainWindow");
    resize(settings.value("size", QSize(400, 400)).toSize());
    move(settings.value("pos", QPoint(200, 200)).toPoint());
    settings.endGroup();
 }



All times are GMT -5. The time now is 05:57 PM.