GTK 3 C++ icon with label in a button - c++

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);
}

Related

Show Window with Shortcut Keybinder and Focus to TextBox GTK4

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);
}
``

how to switch windows between cpp files? in Qt

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();
}

Show the status of icons in toolbar using qt in C++

I would like to show the status of icons in toolbar (whether they are activated). For example, When I click Bold, Italic or Underline icon in Microsoft word, it will be shaded, and switch to normal status when I click it again.
It's not necessary to be shaded. I just need to distinguish whether it is activated.
You have to checkable the QAction, or use a QWidget that is checkable like QToolButton:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QToolBar *toolbar = w.addToolBar("Toolbar");
QAction *bold_action = toolbar->addAction("B");
QFont bold_fn(bold_action->font());
bold_fn.setBold(true);
bold_action->setFont(bold_fn);
bold_action->setCheckable(true);
QAction *italic_action = toolbar->addAction("I");
QFont fn_cursive(italic_action->font());
fn_cursive.setItalic(true);
italic_action->setFont(fn_cursive);
italic_action->setCheckable(true);
QAction *underline_action = toolbar->addAction("U");
QFont fn_underline(underline_action->font());
fn_underline.setUnderline(true);
underline_action->setFont(fn_underline);
underline_action->setCheckable(true);
QAction* subscript_action = new QAction;
subscript_action->setIcon(QIcon(":/subscript.png"));
subscript_action->setCheckable(true); // <---
toolbar->addAction(subscript_action);
w.setCentralWidget(new QTextEdit);
w.resize(320, 240);
w.show();
return a.exec();
}
Output:

No items showing up in menu

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);
}

How to make tab text orientation of a notebook vertical (in gtkmm)

I want my notebook tab labels to be rotated by 90°.
I tried the set_angle() function of Gtk::Label but it doesn't work:
#include <gtkmm.h>
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window mainwindow;
Gtk::Notebook sidebar;
Gtk::Label tab;
mainwindow.add(sidebar);
sidebar.set_tab_pos(Gtk::POS_LEFT);
tab.set_angle(90);
sidebar.append_page(tab, "text");
sidebar.show();
tab.show();
Gtk::Main::run(mainwindow);
return 0;
}
It seems you are adding the label as the child widget. You don't have a widget to be the page child in your code, but if you did and named it "child", you'd have something like this:
sidebar.set_tab_pos(Gtk::POS_LEFT);
tab.set_angle(90);
tab.set_text("text");
sidebar.append_page(child, tab);