I'm building a project like uLauncher where I can pop out a window and focus on the textbox right away. I was able to get the window out using libkeybinder for the shortcut and get the window out with win->show(), but the problem is that the keyboard cursor is in the window behind the window.
How do I get the focus to be on the textbox window?
This is the main code:
void shortcutHandler(const char *keystring, void *user_data) {
winroot->show();
}
int main(int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
winroot = new MainWindow();
auto app = Gtk::Application::create(GTK_ID);
app->hold();
keybinder_init();
keybinder_bind(KEY_SHORTCUT, shortcutHandler, NULL);
return app->run(*winroot);
}
``
Related
I have problem with my small app in Qt framework C++
I have a first window which there's two buttons where you can choose to play music or video. The music button will close the "choose window" and should open "music window" and similarly for video button.
I don't know how to do this... I know a way which I've leant and used but this method I'm going to explain how it doesn't fit to my current issue.
I've created a pointer of that window class in header of choose window and when the music button is clicked, I new the pointer and musicWindow->show(); and hide(); the choose window, this is good but there is a problem:
the new opened music window doesn't have any taskbar icon/thumbnail and when Its minimized there's no way to have that opened again(except wtih alt-tab)
and dont find a way to open it like a complete new window, I just can open all of them at once by using choosWindow.show(); / musicWindow.show(); /... .
I know there must be a way, but I dont even know what topics to search for to get further...
FirstWindow.cpp:
void FirstWindow::on_musicChoose_clicked()
{
//send a signal from here
}
void FirstWindow::on_videoChoose_clicked()
{
//send a signal from here
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FirstWindow w;
if(//what condition?)
{
MusicWindow mw;
mw.show();
}
if(//what condition?)
{
VideoWindow vw;
vw.show();
}
return a.exec();
}
I found my answer in Qt Forum:
https://forum.qt.io/topic/68602/child-window-in-taskbar/3
#Radek(Qt Forum): Try passing 0 (zero) as parent when you create them.
FirstWindow.cpp:
void FirstWindow::on_musicChoose_clicked()
{
this->hide();
mw = new MusicWindow(0); // passing nullptr as parent
mw.show();
}
void FirstWindow::on_videoChoose_clicked()
{
this->hide();
vw = new VideoWindow(0); // passing nullptr as parent
vw.show();
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FirstDialog w;
w.exec();
return a.exec();
}
I am using Ubuntu 20.04 and I have a small GTK 3.0 Form I am trying to get to maximize on first startup. I later want to get this into a Full GLADE project, but first I'm just trying to get this to maximize to full screen.
The commented sections are what I have tried to no avail.
Nothing works and it throws compiler errors, can anyone help me in my endeavor?
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
// gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
// gtk_window_fullscreen(GTK_WINDOW(window);
// gtk_window_fullscreen(GtkWindow *window);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Thank you.
Use gtk_window_maximize(GtkWindow *window):
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
//...
}
Note that you can call it before showing the window. Doc:
Asks to maximize window , so that it becomes full-screen. Note that you shouldn’t assume the window is definitely maximized afterward, because other entities (e.g. the user or window manager) could unmaximize it again, and not all window managers support maximization. But normally the window will end up maximized. Just don’t write code that crashes if not.
It’s permitted to call this function before showing a window, in which case the window will be maximized when it appears onscreen initially.
You can track maximization via the “window-state-event” signal on GtkWidget, or by listening to notifications on the “is-maximized” property.
Suggestion: Use gtkmm with C++
Example with gtkmm:
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.maximize.base");
Gtk::Window window;
window.maximize();
return app->run(window);
}
I am trying to get an icon to appear along side with the label in a button but seems like there is an overriding going on and the button is accepting only one element either label or icon.
here is what i have done so far :
#include <gtkmm.h>
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv);
Gtk::Window* win = new Gtk::Window;
Gtk::Image* icon = new Gtk::Image;
icon->set("src/ui/icons/about.png");
Gtk::Button* btn = new Gtk::Button;
btn->set_label("Hello world");
btn->set_image(*icon);
win->add(*btn);
win->show_all();
return app->run(*win);
}
How can i get both icon and label to show in the button ?
Thanks !
There is a global-wide Gtk setting, gtk-button-images, that distributions or users can use to style applications look. When the value is false, Gtk hides the images in buttons that also have a label.
You can make your button always show the image, regardless of this system setting, by calling gtk_button_set_always_show_image
Here's the updated sample:
#include <gtkmm.h>
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv);
Gtk::Window* win = new Gtk::Window;
Gtk::Image* icon = new Gtk::Image;
icon->set("src/ui/icons/about.png");
Gtk::Button* btn = new Gtk::Button;
btn->set_label("Hello world");
btn->set_image(*Gtk::manage(icon));
btn->set_always_show_image(true);
win->add(*Gtk::manage(btn));
win->show_all();
return app->run(*win);
}
I'm currently haveing a bit of a problem with a Gtk popup menu.
I have a MenuButton set with a Menu which has a MenuItem.
When clicked the Menu pops up but only a tiny bit of it, as if there are no items and doesn't show the Preferences item.
Is there a function I'm not calling or is the MenuItem not being added. As I took a look at program in the Inspector and it doesn't show a Menu anywhere.
I don't want to use the Gtk Builder with Glade just to create a popup menu.
Not sure if this is needed but this is under Msys2 with Gtk 3.22.
#include <gtkmm.h>
class WindowTest : public Gtk::Window {
public:
WindowTest() {
set_titlebar(mHeaderBarApp);
mBoxContainer.set_orientation(Gtk::Orientation::ORIENTATION_VERTICAL);
mBoxContainer.pack_start(mHeaderBarApp, false, true, 0);
mHeaderBarApp.set_show_close_button(true);
mHeaderBarApp.pack_end(mMenuButtonApp);
mMenuButtonApp.set_image(mImageApp);
mImageApp.set_from_icon_name("open-menu-symbolic", Gtk::BuiltinIconSize::ICON_SIZE_BUTTON);
mMenuButtonApp.set_popup(mMenuApp);
mMenuApp.append(mMenuItemPreferences);
mMenuItemPreferences.set_label("Preferences");
add(mBoxContainer);
show_all_children();
};
virtual ~WindowTest() {};
protected:
Gtk::Box mBoxContainer;
Gtk::HeaderBar mHeaderBarApp;
Gtk::MenuButton mMenuButtonApp;
Gtk::Image mImageApp;
Gtk::Menu mMenuApp;
Gtk::MenuItem mMenuItemPreferences;
};
int main(int argc, char *argv[]) {
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.site.test");
WindowTest windowTest;
windowTest.set_default_size(600, 400);
return app->run(windowTest);
}
There is a Qt application. GL-window created into this application by calling XCreateWindow function and I can't edit it.
I need put Xwindow in QWidget inside my Qt applications.
In the documentation:
void QWidget::create ( WId window = 0, bool initializeWindow = true,
bool destroyOldWindow = true ) [protected]
Creates a new widget window if the window is 0, otherwise sets the widget ' s window to window.Initializes the window sets the geometry etc.) if initializeWindow is true. If initializeWindow is false, no initialization is performed. This parameter only makes sense if window is a valid window.
...
For verifying the result of function QWidget::create there is the following code:
class ParentWindow : public QWidget
{
Q_OBJECT
public:
ParentWindow(WId id)
{
create(id);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton* button = new QPushButton("MEGA BUTTON");
button->show();
ParentWindow w(button->winId());
w.show();
return a.exec();
}
When application start a single blank window appears. Although expected window containing a button (or to be a button).
How can I put X11 window into my QWidget?
The problem was resolved:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Display* display = XOpenDisplay(NULL);
XSynchronize(display, True);
XSetErrorHandler(myErrorHandler);
Window x11root = XDefaultRootWindow(display);
int x = 500;
int y = 500;
unsigned int width = 150;
unsigned int height = 150;
unsigned int borderWidth = 0;
long colorBlue = 0xff0000ff;
Window x11w = XCreateSimpleWindow(display, x11root, x, y,
width, height, borderWidth, 1 /*magic number*/, colorBlue);
QWidget w;
w.resize(300, 300);
w.show();
XReparentWindow(display, x11w, w.winId(), 0, 0);
XMapWindow(display, x11w); // must be performed after XReparentWindow,
// otherwise the window is not visible.
return a.exec();
}
To solve the problem through a widget ParentWindow failed - xwindow is embedded in QWidget, but have problems with resizing the window and closing it (it doesn't close).
QX11EmbedContainer could be what you need.
Let Qt create your Window and then use the Qt X11 Drawable with your X11/GL code.
With OpenGL and Qt you must use the Qt OpenGL context, if Qt is rendering using OpenGL. Just be aware that Qt expects the OpenGL state to be put back to what it was when it last used it.
You can get access to the Drawable using QX11Info (also check Compiler does not see QX11Info as this covers a common problem when including X11 with Qt).
The way Qt provides access to both X11 and OpenGL seems to change between major and minor versions so you may need to do a little bit of digging.
I know that the above works with Qt5.1 upto 5.5. Qt5.6 has problems with this approach that I've not yet resolved.
You should not touch window IDs in your first Qt program. Window IDs are a low level concept and a Qt programmer normally needs them only to do something outside of the Qt framework. Managing widgets as children of other widgets is not that kind of task.
I recommend you start with one of the tutorials. Look in particular here to see how you make a widget a child of another widget.