LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2010, 11:34 AM   #1
TheStarLion
Member
 
Registered: Nov 2009
Location: UK
Distribution: Gentoo
Posts: 472

Rep: Reputation: 41
Small QT project


Sorry for the undescriptive title. I'm having a bit of trouble at the moment.
I thought I'd make a small QT-based application, and completely forgot that I have very little experience in C++, which is proving to be a pain.
Now please excuse my rambling attempt to explain.

The application is (Yet another) sudo wrapper, but I kept it relatively simple so I wouldn't confuse myself.
The idea was, sometimes you use a launcher or something, and then realize you needed to sudo it. You could grab a terminal and poke it into that, but I don't like opening a new terminal unnecessarily, so I thought of this.
You launch this app, select from a combobox whether you want to use gksu or kdesudo (Depending on which WM/DE you're using), give it the command in a text box (E.g. 'gedit' 'kate' etc) and hit a button to run that. In theory, gksu or kdesudo would pop up and ask for the password, then the selected app would run as per normal as root.
I've got the interface done (that was the easy part) but having nothing to work from in the coding has proved to be the downfall here.

Basically, I have two functions (I think that's the word, anyway)
One for the quit button - I know you could just use the window manager's one, but I prefer to have one there anyway
The other's for the execute button, which reads the su wrapper to be used from the combobox, the command in the text box, pairs them together and runs them.
It sounds simple, and I've given it a go from the QTCreator help's snippets, and so far managed nothing that would build and run, unless I take out the code I tried to write. Which isn't very useful.

So now I ask if people can lend a hand to my coding block, and help get this working. Maybe not everyone will find it useful, but it's something I want to have around anyway.

Thanks.
 
Old 03-21-2010, 11:41 AM   #2
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
Quote:
Originally Posted by TheStarLion View Post
Sorry for the undescriptive title. I'm having a bit of trouble at the moment.
I thought I'd make a small QT-based application, and completely forgot that I have very little experience in C++, which is proving to be a pain.
Now please excuse my rambling attempt to explain.

The application is (Yet another) sudo wrapper, but I kept it relatively simple so I wouldn't confuse myself.
The idea was, sometimes you use a launcher or something, and then realize you needed to sudo it. You could grab a terminal and poke it into that, but I don't like opening a new terminal unnecessarily, so I thought of this.
You launch this app, select from a combobox whether you want to use gksu or kdesudo (Depending on which WM/DE you're using), give it the command in a text box (E.g. 'gedit' 'kate' etc) and hit a button to run that. In theory, gksu or kdesudo would pop up and ask for the password, then the selected app would run as per normal as root.
I've got the interface done (that was the easy part) but having nothing to work from in the coding has proved to be the downfall here.

Basically, I have two functions (I think that's the word, anyway)
One for the quit button - I know you could just use the window manager's one, but I prefer to have one there anyway
The other's for the execute button, which reads the su wrapper to be used from the combobox, the command in the text box, pairs them together and runs them.
It sounds simple, and I've given it a go from the QTCreator help's snippets, and so far managed nothing that would build and run, unless I take out the code I tried to write. Which isn't very useful.

So now I ask if people can lend a hand to my coding block, and help get this working. Maybe not everyone will find it useful, but it's something I want to have around anyway.

Thanks.
I'm interested in this thread because I'm starting to read through documents on QT, but I can't help but wonder where your code is..

Quote:
It sounds simple, and I've given it a go from the QTCreator help's snippets, and so far managed nothing that would build and run, unless I take out the code I tried to write.
 
Old 03-21-2010, 11:41 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Why not just use an existing launcher and type "kdesu <command>" or "gksu <command>" in it?
 
Old 03-21-2010, 11:53 AM   #4
TheStarLion
Member
 
Registered: Nov 2009
Location: UK
Distribution: Gentoo
Posts: 472

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by MTK358 View Post
Why not just use an existing launcher and type "kdesu <command>" or "gksu <command>" in it?
This is meant to be for the one-off sort of thing where you don't actually run it often. Fair point though, but this is what I was trying to do.

As to the code, I didn't start writing the one for the command, just the quit. I figured it would be simpler and might provide the start of what I needed to figure out the other one.
What I had, based off what I gleaned from the QT help, was the following:
Code:
void MainWindow::on_btnquit_clicked()
{
    void QCoreApplication::exit ( int returnCode = 0 );
}
It wouldn't build, with the error 'Invalid use of qualified-name 'QCoreApplication::exit'
Which suggests to me that I'm either missing something, or I've made a mistake, showing just how little I know about C++ in general.

The second attempt used a snippet I got from a Google search, which is as follows:
Code:
void MainWindow::on_btnquit_clicked()
{
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QPushButton *button = new QPushButton("Quit");
        QObject::connect(button, SIGNAL(clicked()),
                            &app, SLOT(quit()));
        button->show();
        return app.exec();
    }
}
Which in turn told me that a function (I'm guessing the int main part) wasn't allowed before a {
I took a guess and removed the int main line, resulting in a complaint about argc and argv not being declared. Another guess moved the 'int argc, char *argv[]' from the int main to the void MainWindow's brackets, which seemed to be the logical place for them.
That in turn gave me two errors I really don't know anything about, one 'prototype for 'void MainWindow:n_btnquit_clicked(int, char**)' does not match any in class 'MainWindow' and in the corresponding .h file 'candidate is: void MainWindow:nbtnquit_clicked()'

That's what I've tried so far. No doubt it shows off my lack of knowledge over this.
 
Old 03-21-2010, 12:54 PM   #5
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
main is the "main" function, which all programs must contain. It is where your program begins execution.

argc and argv are special variables for commandline arguments.

argc is the number of arguments, and argv[] is an array of arguments entered on the command line at the time of execution.

Quitting or executing things after the execution of the program indicates that command line arguments have been handled a good while ago, and therefore are not relevant to quitting the program, or executing other commands within a running program.

I wonder, for example, if my initial guess on how to write a GUI button that will terminate the program is correct.


From (http://doc.trolltech.com/4.3/qcoreapplication.html#exit)

Quote:

void QCoreApplication::exit ( int returnCode = 0 ) [static]

Tells the application to exit with a return code.

After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.

By convention, a returnCode of 0 means success, and any non-zero value indicates an error.

Note that unlike the C library function of the same name, this function does return to the caller -- it is event processing that stops.

See also quit() and exec().
Code:
void MainWindow::on_btnquit_clicked()
{
    /*
    void QCoreApplication::exit ( int returnCode = 0 );
    */
    return 0;
}


Also, to help you better understand parameters(arguments, values, etc)


Code:
#include <iostream>

//declares function declaredfunction()
int declaredfunction(int parameter1, char parameter2, double parameter3);
// this function takes 3 parameters, int, char, double, in that order.



int main()
{ 
  // these variables are local to main, and are being passed BY VALUE
  // to the function declaredfunction()
  int a = 5;
  char b = '5';
  double c = 5.50;
  
  // notice that there are no datatypes associated with the function call
  std::cout << declaredfunction(a,b,c) << std::endl;

}


int declaredfunction(int parameter1, char parameter2, double parameter3)
{ 
  // makes parameter 1 == 6
  parameter1 += 1;
  // makes parameter 3 == 5, maybe anyway.. 
  parameter3 = (parameter2 - '0'); 
  std::cout << parameter3 << std::endl;
  // returns parameter 1 which is 6. This means that if
  // in main you had int test = declaredfunction(), then test would be 6
  return parameter1;
}

Last edited by Dogs; 03-21-2010 at 01:12 PM.
 
Old 03-21-2010, 08:59 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In Qt you would create a number of methods for each action (check out the documentation for QAction), these will be special methods called slots (again checkout the documentation for Slots and Signals) These slots will then be connected to the GUI widgets that you have selected. This allows the same action to be connected to a button, a menu item and a short-cut key.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need ideas for a small programming project Nylex Programming 6 08-13-2006 12:30 AM
Big project for small N00b HKJGN Linux - Newbie 0 02-14-2005 07:55 PM
Class project, setting up network using Fedora for small startup company. Bronco1 Linux - Networking 2 09-28-2004 03:48 AM
I need some C++ guys/girls, small project karlan Linux - General 2 09-08-2004 07:37 AM
Beginning a big project - Need an Good Project Manager gamehack Programming 3 01-15-2004 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration