No items showing up in menu - c++

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

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

gtkmm - How to make box containing widgets expand to fill window?

I'm new to gtkmm and I'm a bit confused about packing and how to make widgets expand.
This is the code I have written:
#include <gtkmm.h>
class GUIWindow : public Gtk::Window
{
public:
GUIWindow()
{
set_title("title");
set_border_width(10);
set_size_request(800, 600);
add(_box_);
_box_.pack_start(_grid_, true, true, 10);
_grid_.add(_frame1_);
_grid_.add(_frame2_);
_grid_.attach_next_to(_frame3_, _frame1_, Gtk::POS_BOTTOM, 2, 1);
_frame1_.set_label("f1");
_frame2_.set_label("f2");
_frame3_.set_label("f3");
_label1_.set_text("Hello world");
_label2_.set_text("this is an example GUI");
_label3_.set_text("some example text");
_frame1_.add(_label1_);
_frame2_.add(_label2_);
_frame3_.add(_label3_);
show_all_children();
}
private:
Gtk::Box _box_;
Gtk::Grid _grid_;
Gtk::Frame _frame1_;
Gtk::Frame _frame2_;
Gtk::Frame _frame3_;
Gtk::Label _label1_;
Gtk::Label _label2_;
Gtk::Label _label3_;
};
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
GUIWindow guiwindow;
return app->run(guiwindow);
}
I wanted to add 3 labels, each inside their own frame. (Using the frame to see what is going on with the packing.)
I initially added a grid to my window and filled it with the 3 frames, filling each of those with a label. However, the grid did not expand to fill the window. So I tried putting the grid inside a box, but the box also does not expand to fill the window.
I've searched the documentation and the web for a solution, but didn't find one. I think I've just not quite understood how the packing works.
How can I get the "box" object to fill the entire window, so that when the window resizes the box also resizes, and the frames resize with the grid which resizes with the box. (The labels presumably will not resize.)
This is probably what you are looking for
set_halign(Align align)
set_valign(Align align)
set_hexpand(bool expand = true)
set_vexpand(bool expand = true)
An example would be:
for (const auto &child : _grid_.get_children()) {
child->set_hexpand(true);
child->set_halign(Gtk::ALIGN_FILL);
child->set_vexpand(true);
child->set_valign(Gtk::ALIGN_FILL);
}

Change Toolbutton Icon in gtkmm Toolbar after Toolbutton was clicked

I want to change the Icon of a Toolbutton after the Toolbutton was clicked.
My problem is, when i use the Toolbutton method set_icon_widget() , the current Icon disappears, but the new one doesn't show up. The Toolbutton is still there, but it has no Icon anymore.
Here is my Code:
#include <gtkmm.h>
class MainWindow : public Gtk::Window{
public:
MainWindow();
private:
void clicked();
Gtk::Box m_vbox;
Gtk::Image image;
Gtk::Image image_clicked;
Gtk::Toolbar toolbar;
Gtk::ToolButton icon;
Gtk::ToolButton connected;
};
MainWindow::MainWindow() :
image(Gdk::Pixbuf::create_from_file( "network-transmit-receive.svg")),
image_clicked(Gdk::Pixbuf::create_from_file("network-offline.svg")){
//Window Configuration
set_title("Tool Button Icon Test");
set_default_size(400, 200);
set_position(Gtk::WIN_POS_CENTER);
icon.set_icon_widget(image);
connected.set_icon_widget(image_clicked);
icon.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::clicked));
toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
toolbar.set_icon_size(Gtk::ICON_SIZE_SMALL_TOOLBAR);
toolbar.set_vexpand_set(false);
toolbar.add(icon);
m_vbox.set_orientation(Gtk::ORIENTATION_VERTICAL);
m_vbox.pack_start(toolbar, Gtk::PACK_SHRINK, 0);
add(m_vbox);
show_all_children();
}
void MainWindow::clicked(){
icon.set_icon_widget(image_clicked);
}
int main (int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "de.example.Toolbutton-Test");
MainWindow mainwindow;
//Shows the window and returns when it is closed.
return app->run(mainwindow);
}
I also tried to remove the current ToolButton and to add a new one with the different Icon, but than the current Toolbutton is removed and the new one is not drawn :-/
Can somebody help me please?
I just found the answer by myself. I can change the icon of the MenuButton by setting a new image to the image object of the MenuButton.
The clicked Method looks like this now:
void MainWindow::clicked(){
image.set(Gdk::Pixbuf::create_from_file("network-offline.svg"));
}

Why virtual keyboard does not work with QDialog textbox in Qt?

I make an application for keyboard less device.
I used thisRef for keyboard as virtual keyboard.
I have a dialog in my project that has two textboxes (one of them for enter user name and the other for input password ) with two buttons: OK and Cancel. after Build the project and run ,the press menu button to show menu form, then the dialog appears to check user authenticate. the user should enter data on text box the virtual keyboard .
the virtual keyboard (input panel in thisRef ) appears but the buttons not work.
when I searched I saw this "sounds like you are trying to open another window from the dialog - this is your error. Of course the dialog will stay on top - that is its job."
because the dialog is modal, the virtual keyboard is disable
Is there anyway to edit the dialog or keyboard to work in modal widget?
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "myinputpanelcontext.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyInputPanelContext *ic= new MyInputPanelContext; ;
a.setInputContext(ic);
MainWindow w;
//w.show();
w.showFullScreen();
w.centralWidget()->releaseKeyboard();
return a.exec();
}
MainWindow.cpp:
MyDialog *d=new MyDialog(this);
d.exec();
It solved:
Modal dialogs are started with exec(), they block the program flow while a nested event loop runs.
Modeless dialogs are started with show(), they do not block the program flow.
From http://www.qtforum.org/article/14285/modeless-dialog.html
I use this code :
MyDialog *d=new MyDialog(this);
d->show();
d->raise();
q->activewindows();
instead of this code:
MyDialog *d=new MyDialog(this);
d.exec();
as the document reference :
A modeless dialog: void EditorWindow::find() {
if (!findDialog) {
> findDialog = new FindDialog(this);
> connect(findDialog, SIGNAL(findNext()), this, SLOT(findNext()));
> } findDialog->show();
> findDialog->raise();
> findDialog->activateWindow(); }
From here

Qt menubar not showing

I'm building a simple C++ application on Mac OS X 10.9 with Qt 5.2.1 using CMake (without MOC).
I am starting the executable from the command-line. The problem is that the menu bar is not showing up at all, the Terminal menu bar is still visible but not clickable. When I switch windows temporarily and then come back to the window of this application, I at least see the standard "application" menu with "About". The "About" action is now working and shows the dialog. The toolbar button also works as expected.
What else I tried (and didn't work):
using the pre-defined menuBar()
use setMenuBar()
new menuBar(0)
menubar->setVisible(true)
When I check the isVisible() it returns false, also if I set it to visible in the line before.
I wonder whether the lack of using MOC can be the reason for this?
Below I attached a reduced example.
#include <QtGui>
#include <QtWidgets>
class MainWindow : public QMainWindow {
public:
MainWindow();
private:
void create_actions_();
void create_menus_();
void create_toolbar_();
void about_();
QMenuBar* menu_bar_;
QMenu* file_menu_;
QMenu* help_menu_;
QToolBar* file_toolbar_;
QAction* action_about_;
};
MainWindow::MainWindow() {
resize(800, 600);
create_actions_();
create_menus_();
create_toolbar_();
}
void MainWindow::create_actions_() {
action_about_ = new QAction(tr("About"), this);
connect(action_about_, &QAction::triggered, this, &MainWindow::about_);
}
void MainWindow::create_menus_() {
menu_bar_ = new QMenuBar(this);
file_menu_ = menu_bar_->addMenu(tr("&File"));
menu_bar_->addSeparator();
help_menu_ = menu_bar_->addMenu(tr("&Help"));
help_menu_->addAction(action_about_);
menu_bar_->setNativeMenuBar(true);
}
void MainWindow::create_toolbar_() {
file_toolbar_ = addToolBar(tr("File"));
file_toolbar_->addAction(action_about_);
file_toolbar_->setIconSize(QSize(16, 16));
}
void MainWindow::about_() {
QMessageBox::about(this, tr("About"), tr("FooBar"));
}
int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow main_window;
main_window.show();
const int exit_code = app.exec();
return exit_code;
}
CMakeLists.txt
FIND_PACKAGE(Qt5Core)
FIND_PACKAGE(Qt5Gui)
FIND_PACKAGE(Qt5OpenGL)
FIND_PACKAGE(Qt5Widgets)
FIND_PACKAGE(Qt5Declarative)
FIND_PACKAGE(Qt5MacExtras)
ADD_EXECUTABLE(main main.cc)
qt5_use_modules(main Core Gui Widgets Declarative MacExtras)
Thanks a lot in advance!
OK, solved the problem myself. It appears you cannot add a separator to the menubar.
Removing the menu_bar_->addSeparator(); solved the problem.