4

In Qt if you want widgets in a single layout to be separated physically, you add a spacer in between, do we have something like that in GTKmm?
enter image description here

Here the label (Gtk::Label m_label;) and the buttons (Gtk::Button m_open_button, m_delete_button;) are in the same box (Gtk::HBox m_control_HBox;):

m_control_HBox.pack_start(m_label, Gtk::PACK_EXPAND_PADDING);
m_control_HBox.pack_start(m_open_button, Gtk::PACK_SHRINK);
m_control_HBox.pack_start(m_delete_button, Gtk::PACK_SHRINK);

I would like the label to be pushed further left while buttons remain in their place. How do I do that?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Could you be more precise with "further left"? (All the way to the left? Space it as if there are four widgets in the box? Add 20 pixels regardless of how the box gets resized? Something else?) – JaMiT Jul 24 '19 at 21:55
  • @JaMiT All the way to the left. That's what I meant – Aykhan Hagverdili Jul 25 '19 at 05:19
  • @JaMiT Did my comment clarify the problem or do you have further questions? – Aykhan Hagverdili Jul 26 '19 at 17:04
  • Your comment clarified it. I just have limited time for StackOverflow during the week (and I wanted to check the code in my answer since I don't have the Gtkmm API memorized). – JaMiT Jul 27 '19 at 01:19

2 Answers2

4

Currently you have the label widget as wide as necessary to accommodate the text, and the widget is centered in the available space. (The spaces to the sides of the label are part of the HBox.)

-----------------------------------------------
|        | Label |        | Button || Button ||
-----------------------------------------------
 ^^^^^^^^         ^^^^^^^^
These spaces are the padding that was expanded, as requested.

To get the text all the way to the left, you need to change that setup. I can provide two options.

Expand the widget and move the text

It might be useful (for mouse events?) to have the label widget fill the available space, completely hiding the HBox.

-----------------------------------------------
|| Label                 || Button || Button ||
-----------------------------------------------

This can be accomplished by changing its packing from Gtk::PACK_EXPAND_PADDING to Gtk::PACK_EXPAND_WIDGET.

By itself, though, this change won't appear to have done anything, since the text is by default centered in the label widget. So you will also want to call set_xalign(). The parameter to this function is a floating point value from 0 to 1, where 0 is far-left and 1 is far-right.

Your code would look something like the following.

m_label.set_xalign(0.0f);                                    // <-- set text alignment
m_control_HBox.pack_start(m_label, Gtk::PACK_EXPAND_WIDGET); // <-- change packing
m_control_HBox.pack_start(m_open_button, Gtk::PACK_SHRINK);
m_control_HBox.pack_start(m_delete_button, Gtk::PACK_SHRINK);

Move the label

Another option would be to keep the width of the label widget, but move it all the way to the left.

-----------------------------------------------
|| Label |                | Button || Button ||
-----------------------------------------------
          ^^^^^^^^^^^^^^^^
This space within the HBox has no widgets covering it.

To start, you're going to give the label the same packing as the buttons, Gtk::PACK_SHRINK. The trick here is to keep the buttons to the right. This is the purpose of the pack_end() function. Pack the label from the start, and the buttons from the end.

m_control_HBox.pack_start(m_label, Gtk::PACK_SHRINK);       // <-- Change packing
m_control_HBox.pack_end(m_open_button, Gtk::PACK_SHRINK);   // <-- pack_end
m_control_HBox.pack_end(m_delete_button, Gtk::PACK_SHRINK); // <-- pack_end
JaMiT
  • 14,422
  • 4
  • 15
  • 31
0

Did you try using one of the add_label methods? They allow you to set the alignment of your label.

From here:

void add_label (const Glib::ustring& label, bool mnemonic=false, double x_align=0.5, double y_align=0.5)
    Add a Label object.

void add_label (const Glib::ustring& label, bool mnemonic, Align x_align, Align y_align=ALIGN_CENTER)
    Add a Label object.
YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
  • They are member functions of `Gtk::Button`. I want to align the label within a `Gtk::Box` – Aykhan Hagverdili Jul 09 '19 at 19:36
  • Do you just want left, right, center alignment, or an exact pixel position? Either way, I would not put the label directly into the parent box. I would put a child box into the parent box, so you can change the alignment of the label into this child box. And of course only show the label of the child box, not the box itself. – YesThatIsMyName Jul 10 '19 at 06:40
  • Just left/right alignment. Can you demonstrate what you said with a bit of code in your answer? – Aykhan Hagverdili Jul 10 '19 at 19:13