jmhobbs

GTK Tooltips On Notebook Tab Labels

Here's a non-obvious (or to me at least) trick to get tooltips onto a Gtk::Notebook tab. It took some searching, but essentially, you just add the Gtk::Label to a Gtk::EventBox and add that to the tab instead. Then you attach the tool tip to the Gtk::EventBox instead of the Gtk::Label.

Here's an example. I couldn't get my example to compile, the linker was going crazy, but I'm 99% sure that it's fine. I'm probably just not seeing one glaring error. Let me know if you find it. The important stuff is all there, even if it won't build.

main.cpp

#include 
#include "nbtt.h"

using namespace std;

int main (int argc, char *argv[]) {

  Gtk::Main kit (argc, argv);
  Nbtt notebookWindow;
  Gtk::Main::run(notebookWindow);
	
  return 0;
}

nbtt.cpp

#include 
#include "nbtt.h"

using namespace std;

Nbtt::Nbtt() {

  set_title("Notebook Tabs With Labels!");
  set_border_width(10);
  set_default_size(400, 200);

  lblTabOne.set_text("Tab 1");
  lblTabTwo.set_text("Tab 2");
  lblTabThree.set_text("Tab 3");

  ebTabOne.add(lblTabOne);
  ebTabTwo.add(lblTabTwo);
  ebTabThree.add(lblTabThree);
	
  toolTips.set_tip(ebTabOne,"Tab to page one.");
  toolTips.set_tip(ebTabTwo,"Tab to page two.");
  toolTips.set_tip(ebTabThree,"Tab to page three.");

  exNotebook.append_page(pageOne, ebTabOne);
  exNotebook.append_page(pageTwo, "Second");

  show_all();
}

nbtt.h

#ifndef NBTT_H
#define NBTT_H

#include 
#include 
#include 
#include 
#include 

using namespace std;

class Nbtt;

class Nbtt : public Gtk::Window {

  public:
    Nbtt();
	
  private:
    Gtk::Notebook exNotebook;
		
    Gtk::EventBox ebTabOne;
    Gtk::EventBox ebTabTwo;
    Gtk::EventBox ebTabThree;

    Gtk::Label lblTabOne;
    Gtk::Label lblTabTwo;
    Gtk::Label lblTabThree;

    Gtk::Tooltips toolTips;

    Gtk::Label pageOne;
    Gtk::Label pageTwo;
    Gtk::Label pageThree;
	
};

#endif // NBTT_H

I found this trick in the [email protected] history. The thread starts here if you want to read the exchange.