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
Related
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.
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.
I have a QGLWidget as part of the UI of my application. It is NOT a central widget, there are a lot of others widgets around it. I want to show it full screen on user clicks the button. Similar functionality like on youtube video flash player.
I have tried to use showFullScreen with no effect.
I have read how-to-fullscreen-a-qglwidget and fullscreen-widget, but they suggest using showFullScreen.
Qt documentation states that for using showFullScreen widget must be an independent window. So I assume there should be some trick for this.
The solution I found:
void MyApp::on_fullscreen_button_clicked() {
QDialog *dlg = new QDialog(this);
QHBoxLayout *dlg_layout = new QHBoxLayout(dlg);
dlg_layout->setContentsMargins(0, 0, 0, 0);
dlg_layout->addWidget(glwidget_);
dlg->setLayout(dlg_layout);
dlg->showFullScreen();
bool r = connect(dlg, SIGNAL(rejected()), this, SLOT(showGlNormal()));
assert(r);
r = connect(dlg, SIGNAL(accepted()), this, SLOT(showGlNormal()));
assert(r);
}
void MyApp::showGlNormal() {
ui.glBox->layout()->addWidget(glwidget_);
}
The showFullScreen function works only on windows. From the Qt documentation:
A window is a widget that isn't visually the child of any other widget
and that usually has a frame and a window title.
A possible solution is the following:
When the user clicks the show full screen button
Create a new QGlWidget with no parent and set to it the contents of you QGlWidget
Use the showFullScreen function on it...
Maybe it is a better idea to subclass QGlWidget and pass in its constructor a pointer to another QGlWidget. The constructor should take the context of the provided widget and apply it to the new one.
On your subclass catch keyboard events. When the user press Esc emit a signal
In your base class catch this signal and connect it to a slot. In this slot hide the full screen QGlWidget and delete it.
I have a class extending QWidget class and this class uses a Ui class generated by QtDesigner. Something like following:
class MyWidget : public QWidget
{
signals:
void pushButtonClicked();
// ....
private:
Ui::MyUi ui;
}
MyWidget::MyWidget()
{
ui = new Ui::MyUi();
ui->setupUi(this);
}
Now lets suppose there is a QPushButton * pushButton field inside Ui::MyUi. The UI for this widget has no complex business logic.
Now it is required that whenever pushButton is clicked this widget should emit a signal (lets call it pushButtonClicked(). Now the only way I can imagine this to be achieved is to:
connect clicked() signal of pushButton object to a local slot and emit signal pushButtonClicked()from there.
Install MyWidget as mouseEventFilter for pushButton and handle it inside MyWidget::mouseClickedEvent
Are there other options? Like somehow letting Qt framework to use 'MyWidget::pushButtonClicked()' instead of pushButton->clicked()
In the project I have many scenarios like this where such signal passing is needed from the wrapper classes like MyWidget, which wrap/abstract the UI by aggregation or inheritance. In general which is considered the best approach for better design, code reuse esp. when the project is at nascent stage and it is not known which of these UIs might need complex business logic in the future.
Please note that extending QPushButton is not an option.
You can connect signals directly =)
connect(pushButton , SIGNAL(clicked()), this, SIGNAL(pushButtonClicked()));
Currently Im using QT Creator which created UI file for view and I'm a QT starter.
There is a part I am curious is that how can i create another Class for, let say a GraphicView, so that I can send signal and slot to that class instead of the main form?
Side Question: why I can't edit Singal and Slot in other place than MainWindow in Edit Signal/Slot Mode? (the edit button is not activated if it's not MainWindow, so i have to use those default Signal) Let say i need to create a ToggleFullScreen() but the edit is gray out, how do I do it?
Adam is right.
But there are several ways to use a UI file in your application.
Have a look at http://qt.nokia.com/doc/4.5/designer-using-a-ui-file.html
For signal-slot specific question : see the link in Adam answer.
But, in summary, if you declare the Q_OBJECT macro in yours classes, you can communicate between those by signal-slot mechanism !
Signals and Slots
You have to create a derived class that inherits from QMainWindow if you want to add new signals or slots.