How to check if a Mainwindow button is clicked from main.cpp? - qt

Is there any way to check if the Mainwindow button is clicked from main.cpp? I want to excecute the following line when I pressed the Play button of Mainwindow:
pipe = popen("mplayer -slave '/home/cinesoft/Desktop/yamuna/songs/first.3gp' &", "w");
I also want the following line to execute when the pause button is pressed:
fputs("pause\n", pipe);

bjoernz is right. You need to use the Signals and Slots mechanisms built into QT.
Why can't you execute that code in the Mainwindow.cpp class instead of main.cpp? Would make life a lot easier.

You may want to look under Signals & Slots on this page:
Programming with Qt
Cross-Platform Applications using Qt is also a good reference for Qt programming.

Related

How to simulate the cancel button pressing for QFileDialog?

How to simulate the cancel button pressing for QFileDialog?
Searching doesn't help much here as most threads (like this one) are talking about simulating key pressing for QT application, not particularly for a QFileDialog.
P.S.: I want to do this because currently I am using a library created by others, the code somehow depend on the QFileDialog to open. It is becoming very annoying if needing user intersection each time by manually pressing the cancel button.
Any help is appreciated.
I assume the library uses QFileDialog as shown in the documentation:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
QStringList fileNames;
if (dialog.exec()) {
fileNames = dialog.selectedFiles();
}
In that case you can just hide it. It causes exec() to return QDialog::Rejected immediately:
fileDialog->hide();

Calling a form from a pushbutton using only QtDesigner

I have created a form. I have many 2 push buttons. On clicking on a pushbutton I want to call another custom form. I am using only QtDesigner. I am NOT using QtCreator. Using QtCreator, there are so many examples on how I can do it. But using only QtDesigner 4 there are no examples. I have also tried creating a MainWindow and then having pushbuttons in that. I want to call a new pop up window when I click on a button (which is a custom form). I am using Eclipse CDT as the IDE. I have installed Qt plugin so that I can do both C++ and Qt development. The problem is I cannot use 'Form' to declare my custom form in header file of the mainwindow.
I read in few posts that this is not possible to do using only QtDesigner and also read it can be done using QObject::connect. Please can anyone help me to confirm if we can do it and if yes please can you provide me an example?
Yes, it's definitely possible with C++. You'll need to connect() pushbutton's clicked() signal with a slot in your first form:
connect(pushButton, SIGNAL(clicked()), this, SLOT(show2ndForm()));
The good place to connect is in your first form constructor.
In that slot just show your second form (for example, using QDialog::exec()):
void FirstForm::show2ndForm()
{
static SecondForm *form = 0;
if(!form)
form = new SecondForm(this);
form->exec();
}
You'll probably need to inherit your second form from QDialog to use this method and also create header and source files for your second form.
For modeless form instead of modal, use form->show() instead of exec().
This is not complete possible if you need to customize a slot, but for simple operation where the existing slots are available, you can just use the signal-slot edit as below.
You can select the receiver object and then the correponding slot. I am providing further screenshot to show it is done for customized slots as well.
Right click with your mouse in the middle of the main window and the change signals and slots
Select the change signals and slot option
Use the Add button to add a new slot
Click on the OK button to conirm it once you chose the desired name
In the signal-slot editor double click on the desired object's slot, and it will show the available slots including your new custom slot.
Select your custom slot and you are done from the designer parts. Do not forget to actually implement that in your C++ code.
If you do not need a custom slot, and a built-in will suffice, you can select that off-hand without the previous steps. Those are provided for completeness.

QT creating push buttons that add text to a text edit box

New to QT just playing around with it to see if its something I will enjoy using and if so would like to go on and learn the program in depth.
Struggling a bit with the button concept. I have created a button and a textedit area. I want to add a string of text into the textedit window when the button is pressed.
I can't seem to find anything on google or the QT wiki to achieve this. Can someone point me in the direction so I can at least get started and have a play with this great tool.
In Qt signals and slots are being used to communicate between the objects. This should provide you with the necessary information to get you started.
A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal. Qt's widgets have many pre-defined slots, but it is common practice to subclass widgets and add your own slots so that you can handle the signals that you are interested in.
So, in your particular case you need to connect the QPushButton clicked() signal with your custom slot that does what is needed (add the text to the textarea):
QPushButton * btn = new QPushButton("Button", this);
connect(btn, SIGNAL(clicked()), this, SLOT(onBtnClicked()));
And we need to declare our slot in the header:
private slots:
void onBtnClicked();
And define it:
void MySpecialWidget::onClick()
{
// Do what is to be done
}
If you have done everything correctly it should work... Otherwise have a look at the console to see if there are any messages looking like:
Object::connect: No such slot MySpecialWidget::onClick() in ...
or
Object::connect: No such signal ....
They should give you a hint about what is going on.
Finally I recommend to have a look at the broad set of Qt examples.

Connecting QDialog to QMainWindow

I have created a QMainWindow using QT Designer. I have components that have signals slots. I've made some non-gui components QObjects so I can also use the signal/slot mechanism.
My next step was to add a modeless dialog. I do not want the dialog to block the main window. I would like the dialog to remain active(somewhere visible to the user or in the background) until the user clicks to close it. I created a modeless QDialog object with designer. The dialog pops up as needed. It keeps the information from the previous session in the text box as well. It's just a filename.
However, when I started to connect the QDialog to the QMainWindow, the signal/slot mechanism does not appear to work.
This is my code:
//In the .h file declared as member of the QMainWindow
ScriptDialog *theScriptDialog;
//In the .cpp file, the constructor of QMainWindow
theScriptDialog = new ScriptDialog(this);
QObject::connect(theScriptDialog, SIGNAL(notifyStartScript(QString)), SLOT (startScripting(QString)));
Maybe I am barking up the wrong tree. Is there a better way to create a pop up window like this?
First see if the connect succeeds: check applications console output for a warning about failed connect, then print return value of connect call.
Then some things to do:
You have the Q_OBJECT macro in scriptdialog.h
You remembered to run qmake after adding new Q_OBJECT classes
Add qDebug print/breakpoint to where notifyStartScript is emitted
Connect something else to startScripting slot, with qDebug print or breakpoint, to verify it gets called
If you have used in-source builds (like just running qmake and make from command line at source dir), then switched to 'shadow build' in separate build dir, make sure the source dir is clean of generated .cpp, .h and .o files.

How to generate a window (widget) on button press in qt

I have designed a GUI through Qt creator on Linux. This design consists of some fields, text edit and some push buttons.
When I press on the push button I want to display another window. Is there any GUI option for this or any hard code?
You need signals and slots.
You have to connect the clicked signal to a custom slot, created by you, of your main widget.
Corrected Code, based in the comments of Patrice Bernassola and Job.
In the class definition (.h file) add the lines:
Q_OBJECT
private slots:
void exampleButtonClicked();
private:
QDialog *exampleDialog;
The macro Q_OBJECT is needed when you define signals or slots in your classes.
The variable exampleDialog should be declared in the definition file to have access to it in the slot.
And you have to initialize it, this is commonly done in the constructor
ExampleClass::ExampleClass()
{
//Setup you UI
dialog = new QDialog;
}
In the class implementation (.cpp file) add the code that does what you want, in this case create a new window.
void ExampleClass::exampleButtonClicked()
{
exampleDialog->show();
}
And also you have to connect the signal to the slot with the line:
connect(exampleButton, SIGNAL(clicked()), this, SLOT(exampleButtonClicked()));
Your question is somehwat basic, so I suggest to read a basic tutorial, in this way you can make progress faster, avoiding waiting for answers.
Some links to tutorials that were useful to me:
http://zetcode.com/tutorials/qt4tutorial/
http://doc.qt.io/archives/qt-4.7/tutorials-addressbook.html
on click event of button you create another widget and show.
another option is Stacked widget, http://doc.trolltech.com/4.6/qstackedwidget.html

Resources