How can I use gtk_window_set_transient_for ()? - c++

I am using the library gtkmm. My code is almost perfect, I think because it compiles and I can execute it. But in the terminal when I clicked on open a file in my software that I made with gtkmm I can read this message :
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
So I looked for on this forum how can I solve it and I understood I have to use this method : gtk_window_set_transient_for().
Actually I have a Gtk::Window and a Gtk::FileChooserDialog. Can you put an example which use gtk_window_set_transient_for() ?
Thank you very much !

Gtk::FileChooserDialog and other GTK+ dialogues are derived from Gtk::Window. Gtk::Window has the method set_transient_for(Gtk::Window &parent); which if not set gives you the message you have seen.
To fix this set_transient_for(Gtk::Window &) needs to be used. Note that this takes a reference and not a pointer. So you would use it something like this.
{
Gtk::Window first_window;
...
Gtk::FileChooserDialog file_dialog("Title");
...
file_dialog.set_transient_for(first_window);
...
}
It is also possible to set the transient window for the dialog with the constructor. like so.
{
Gtk::Window first_window;
...
Gtk::FileChooserDialog file_dialog(first_window, "Title");
...
}
If first_window is a pointer the you would need to do something like so.
file_dialog.set_transient_for(*first_window);

Related

How to get the XId of a Gtk::Window in Gtkmm4

I am using Gtkmm4, and lots of API has been removed. In Gtk3 one could use Gtk::Window::get_xid (inherited from Gdk::Window), but thats not possible anymore, because Gdk::Window has been renamed to Gdk::Surface, and Gtk::Window seems to not inherit from it anymore. But, one can always get the XId of a Gdk::Surface using GDK_SURFACE_XID(surface). How can i get the Gdk::Surface of a Gtk::Window, or alternatively, get the XId from a Gtk::Window directly ?
Note : i need only solutions using Gtkmm 4, not Gtkmm 3 !
The code that deals with the underlying surfaces got split off into a separate interface, Gtk::Native, which Gtk::Window then implements. Gtk::Native has a method get_surface() which should just work like Gtk::Window::get_window() did in GTK 3.

Create a window of custom class type in Qt

This is my first question here, so I'm trying to not sound stupid!
EXPLANATION:
I have a main window in Qt that has a button to create (sub-?) windows within the main window. This can be done as many times as the user wants, and each sub-window displays the same set of properties/items. I figured writing a class to hold all these properties would be a smart way to go about it (this would inherit the main window class), as each instance of the child window would automatically get the properties. I am using a slot to create each instance.
QUESTION:
Besides the desired properties, what do I add to the child window class to let Qt know that if I create an object of that type it should open a window?
For example, say I have implemented all the child window properties in a header file that looks something like this:
#include <QObject>
#include <QDialog> //Not sure about this
class ChildWindow : public ParentWindow
{
Q_OBJECT
public:
ChildWindow(QObject* parent);
~ChildWindow();
//Remaining properties like QSpinBox, Radio buttons etc
}
how then would I implement my slot? Like this?
void Parent::Slot()
{
ChildWindow* window;
window = new ChildWindow(this);
window->show()
}
My problem is that I don't see any code that indicates that window is a separate window. I can see that it is of type ChildWindow, but does just including QDialog give it the show() functionality?
EDIT:
I realise the first suggestion would be try and see if this works, but in the unlikely scenario that it works I wouldn't have learnt anything and I still wouldn't know why it worked, and if it didn't I would be back here asking this same question. I hope you guys understand.
EDIT 2:
error C2039: 'show' : is not a member of 'ChildWindow'
So I am guessing including QDialog did not do the trick
EDIT 3:
If I add this to the ChildWindow constructor
QDialog* child;
child = new QDialog;
child->show()
Do I have to do the same in the slot definition as well?

Qt problem when working over classes GUI access

I got 2 classes:
- MainWindow (Was the default class)
- ExtraClass (That i created myself)
Inside the class MainWindow i've made a public function called "logger". This function looks like this:
// Takes in a QString and appends it to a QTextEdit.
void MainWindow::logger(QString Log_MSG)
{
ui->Logg->append(Log_MSG);
}
This logger functions works out as expected inside its own Class MainWindow but when i try to pass in a MSG into logger from the class ExtraClass, it suddenly doesn't work.
My approach to accessing logger from MainWindow to ExtraClass:
MainWindow Con;
Con.logger("The Message the will get appended to ui->logg");
So the question, what have i missed? I don't get any errors and the text "Log_MSG" that should be appended to the QTextEdit Log don't execute.
Sorry for the style, i just don't understand how to get it to look good.
EDIT:
I've already tried to access other functions from "MainWindow class"
and that works but when i try to pass a string this particuallry function "logger"
from another class nothing happens.
For an instance:
MainWindow MainWindow;
int ANumber = MainWindow.GiveMeAValue(); // This works
But when i'm doing this:
MainWindow MainWindow;
MainWindow.logger("Log MSG"); // This dosen't work
My guess is that the problem lies in the appendment of
a QString passed in into the main class that was automatically created by Qt (have stuff like ui->abc) from another class. But in my current
level of understandment of Qt i don't really know where to
troubleshoot beocuse i don't even get an error.
Your code to access the logger is wrong (it shouldn't even compile).
First, everytime you call the function where this code resides, you create a new local MainWindow object (Con). And then you try to call the method on the class and not on an object. If it is a static method (which I doubt, due to the use of ui), you would have to write MainWindow::logger(). If it is not a static method, then you need to call it on a specific MainWindow instance. But instead of creating a local MainWindow everytime, you should provide the correct application's MainWindow instance to your ExtraClass object.
If all this sounds alien to you, you should first look a bit deeper into fundamental C++ programming before delving into Qt.

How would I open a new window from a button in the main window using Qt Creator?

This seems like a simple task, but I haven't been able to figure out how I would do it. I have two windows designed in Qt Creator, one of which is meant to open when a button is pressed in my main window. Here is the code I am trying to use to open it:
void MainWindow::on_generateDomain_clicked()
{
DomainGeneration dg;
dg.show();
}
DomainGeneration is the name of my window's class. The header and source code for this have not been altered from the default Qt Creator generated for me. Am I doing something wrong? I don't get any errors, the window just doesn't open when the button is pressed.
{
DomainGeneration dg; // <-- automatic object
dg.show(); // equivalent to setVisible(true)
} // at this point dg is destroyed!
One solution is to make dg a (private) data member of the MainWindow class.
QDialog has open() and exec() methods which show the dialog as a modal dialog. Probably you assumed that it was the default behavior. In your case though, dg gets created and destroyed immediately.
This is more of a "thank you" to Nick Dandoulakis than it is an answer. That was so helpful. I'm such a noob I would have never thought about the object being destroyed after the method ends.
I declared (or instantiated... or both?) my about class in the header file for my main window (window.h), then added the following functionality to the slot in window.cpp:
void Window::on_actionAbout_triggered()
{
Window::about.show();
Window::about.raise();
Window::about.activateWindow();
}
I guess this works because the about object is previously instantiated and so isn't limited to the scope of the method or slot which terminates quite quickly.

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try and load his form:
//connect buttons and such
connect(exitbtn, SIGNAL(triggered()),this,SLOT(terminated()));
connect(add, SIGNAL(triggered()),this,SLOT(add_rec()));
void MainWindowImpl::add_rec()
{
//form quits as soon as it loads...?
DialogImpl dia;//name of his form
dia.show();
}
I have included his header file. The program compiles but when I hit the trigger his form loads up for maybe half a second and then closes. Does anyone know what I am doing wrong?
You have almost get it right. This is because the RAII of C++. If you allocate the Dialog on stack, it would be destructed as soon as the function return.
Assuming MainWindowImpl inherits publically from QWidget, you're looking for this:
void MainWindowImpl::add_rec()
{
// passing "this" to the constructor makes sure dialog will be cleaned up.
// Note that DialogImpl will need a constructor that takes a
// QObject* parent parameter.
DialogImpl* dialog = new DialogImpl(this);
dialog->show();
}
Look at the Qt documentation for examples of how the constructors should look.
Apparently QT4 only allows one instance of an object at a time, however pointers are another matter. Change both the main.cpp and what ever your main window to look something like this:
DialogImpl *dia=new DialogImpl;
dia->show();