Reply
Thread Tools
Posts: 353 | Thanked: 263 times | Joined on Dec 2009 @ Finland
#11
Originally Posted by D-Iivil View Post
Thanks for this, but how do I put the input from those select boxes as arguments?

And in which part of my project I should put these:
Code:
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(hello()));
Code:
void MainWindow::hello()
{
    QStringList arguments;
    arguments << "Enable custom transitions" << "Droid Sans" << "Black";
    QProcess::execute("/path/to/my/script/script.sh", arguments);
}
I have these:
Forms:
- mainwindow-ui

Headers.
- mainwindow.h

Sources:
- main.cpp
- mainwindow.cpp
Put the connect inside MainWindow's constructor (function named MainWindow::MainWindow() )in mainwindow.cpp and the function hello also in same file but not inside anything. You'll probably also need to put some includes in the beginning of the file:
Code:
#include <QProcess>
#include <QStringList>
__________________
My Maemo5 projects:
mSpede - Speed testing game | Them Bloody Ducks - 2D duck hunting game | Maetronome - A simple metronome app | CuteMPC - MPD client
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#12
Originally Posted by TNiga View Post
Put the connect inside MainWindow's constructor (function named MainWindow::MainWindow() )in mainwindow.cpp and the function hello also in same file but not inside anything. You'll probably also need to put some includes in the beginning of the file:
Code:
#include <QProcess>
#include <QStringList>
After doing these I get build issue described as:
no 'void MainWindow::hello()' member function declared in class 'MainWindow'

The mainwindow.cpp -file is now like this:
Code:
#include <QProcess>
#include <QStringList>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(hello()));
    delete ui;
}

void MainWindow::hello()
{
    QStringList arguments;
    arguments << "Enable custom transitions" << "Droid Sans" << "Black";
    QProcess::execute("/path/to/my/script/script.sh", arguments);
}
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#13
Originally Posted by D-Iivil View Post
Thanks for this, but how do I put the input from those select boxes as arguments?
You can reach the widgets on the .ui with the ui variable in your cpp code.
Eg.

Code:
ui->myComboBox->doStuff();
About what do do with a combo box, here is some doc:
http://doc.qt.nokia.com/4.6/qcombobox.html
Qt is _very well_ documented, you should be able to find anything you want to know about it!

Originally Posted by D-Iivil View Post
And in which part of my project I should put these:
The easiest way:

In Qt Creator's UI designer, right-click a widget, click "Go to slot...", select a slot (eg. clicked() for a button), and it generates what you need where it needs to be.
 

The Following User Says Thank You to Venemo For This Useful Post:
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#14
Originally Posted by TNiga View Post
You'll probably also need to put some includes in the beginning of the file:
Code:
#include <QProcess>
#include <QStringList>
Simplified:

Code:
#include <QtCore>
#include <QtGui>
These will include everything you'll need.
 

The Following User Says Thank You to Venemo For This Useful Post:
Posts: 353 | Thanked: 263 times | Joined on Dec 2009 @ Finland
#15
Originally Posted by D-Iivil View Post
After doing these I get build issue described as:
no 'void MainWindow::hello()' member function declared in class 'MainWindow'
You need to add declaration to mainwindow.h
Code:
class MainWindow : .....
{
   // ....

   public slots:
      void hello();
   
  // ....
};
EDIT: And your connect seems to be now in wrong place, it's in the destructor (MainWindow::~MainWindow()) though it should be in constructor (MainWindow::MainWindow())
__________________
My Maemo5 projects:
mSpede - Speed testing game | Them Bloody Ducks - 2D duck hunting game | Maetronome - A simple metronome app | CuteMPC - MPD client
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#16
Originally Posted by Venemo View Post
You can reach the widgets on the .ui with the ui variable in your cpp code.
Eg.

Code:
ui->myComboBox->doStuff();
About what do do with a combo box, here is some doc:
http://doc.qt.nokia.com/4.6/qcombobox.html
Qt is _very well_ documented, you should be able to find anything you want to know about it!
Okay, I got the apply -button to launch the script. Still haven't figured out how to get the text string from drop down menus to be put as variables.
After that it should be pretty much done.

I found this, but don't know what to do with it
http://doc.qt.nokia.com/4.6/qcombobox.html#activated-2
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#17
Originally Posted by TNiga View Post
EDIT: And your connect seems to be now in wrong place, it's in the destructor (MainWindow::~MainWindow()) though it should be in constructor (MainWindow::MainWindow())
Yeah.
A little education:
- The destructor runs when an object (in this case, your MainWindow) is destroyed - it is the place where you should free up your memory.
- The constructor is called when your object is about to be created.

About the connect() calls - it is a good practice to put them into the constructor. However, for items in the .ui, it is best to create the slots with the designer and let it do this for you.
(That way, the connect() is taken care of by the designer and you don't have to call it yourself.)
 
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#18
Originally Posted by D-Iivil View Post
Okay, I got the apply -button to launch the script. Still haven't figured out how to get the text string from drop down menus to be put as variables.
After that it should be pretty much done.

I found this, but don't know what to do with it
http://doc.qt.nokia.com/4.6/qcombobox.html#activated-2
Well then, step-by-step:
  • Create a private variable of type QString in your header file in your class
    Code:
    QString myString;
  • In your constructor, set a default value to this variable.
    Code:
    myString = "mydefaultstuff"
  • In the designer, right click the combobox, click "go to slot...", and select the slot you've found in the doc
  • in the generated method, assign your variable - set its value to the one that is returned by the slot
    Code:
    myString = text;
  • In your button's clicked(), use the value of this variable.

Note that there should be some function which returns the current item in the combo box - using that in your clicked() should eliminate the need of introducing the variable.
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#19
Originally Posted by Venemo View Post
Yeah.
About the connect() calls - it is a good practice to put them into the constructor. However, for items in the .ui, it is best to create the slots with the designer and let it do this for you.
(That way, the connect() is taken care of by the designer and you don't have to call it yourself.)
For me it does not seem to create the connect() when right-clicking the button in UI and choosing -> Go to slot...
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Posts: 180 | Thanked: 76 times | Joined on May 2010
#20
Originally Posted by D-Iivil View Post
Okay, I got the apply -button to launch the script. Still haven't figured out how to get the text string from drop down menus to be put as variables.
After that it should be pretty much done.

I found this, but don't know what to do with it
http://doc.qt.nokia.com/4.6/qcombobox.html#activated-2
If you just want to get text from drop down menu when button is clicked (put this in hello() slot):
Code:
ui->myComboBox->currentText(); //This property holds the text of the current item.
Or if you want to do something when user changes selection in drop down menu (put this in constructor and create new slot):
Code:
connect(ui->myComboBox, SIGNAL(currentIndexChanged(QString),
this,
SLOT(comboBoxCurrentIndexChanged(QString));
Code:
void MainWindow::comboBoxCurrentIndexChanged(const QString &text) {
   //Do something
}
E: Bit late.
 
Reply


 
Forum Jump


All times are GMT. The time now is 20:09.