What does `class HelloWorld : public Gtk::Window` mean? - c++

I'm following the Gtk "Hello World" tutorial found here, and I've come across a line in a class declaration I've never seen before (I've only been learning to program for a few months now), and I was wondering if someone could please explain it to me. The line is the
class HelloWorld : public Gtk::Window
I know what class HelloWorld is doing, but I've never seen the public Gtk::Window before. The full header file is provided for reference.
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H
#include <gtkmm/button.h>
#include <gtkmm/window.h>
class HelloWorld : public Gtk::Window
{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
//Signal handlers:
void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
#endif // GTKMM_EXAMPLE_HELLOWORLD_H

It means that HelloWorld is derived from Gtk::Window, so it inherits its behaviour.
HelloWorld represents a Gtk window, so it is just natural to have it derive from the Gtk's window class. It's constructor will probably add a button to the window (the actual window is created by the parent class constructor, which is invoked automatically when a new instance of HelloWorld is created …) and connect a signal handler (on_button_clicked) to the window.
You can call all of Gtk::Window's methods through an instance of HelloWorld. In turn, HelloWorld can override virtual methods of Gtk::Window to change its behaviour.

class HelloWorld : public Gtk::Window
This means the class HelloWorld is publically derived from class Window defined inside Gtk namespace. Gtk::Window is the fully qualified name of that class.

I just want to point out that you should either be using the 3.0 branch of gtkmm or you should use the stable branch of the tutorial.link text
The 3.0 branch of gtkmm is still under development, and you should expect a few "surprises" now and then.

Related

How to add QwtPlot as a base class in Qt promote?

I am using Qt creator 4.2.1. I have installed Qwt lib successfully and been able to drag and drop QwtPlot widget to my form. I have an custom class Plot:public QwtPlot. I wish I can add QwtPlot to my form and then perform a widget promote to Plot.
However, when I try to do so, the promote dialog box does not show QwtPlot as a base class that I can choose. If I select QWidget as the base class and then specify "Plot" and "plot.h" as my promote class and header file, I got build errors complaining redefine of "class Plot". I do have the plot.h in my project already and it seems uic creates another "class Plot" for me. Hence, the compiler complains my plot.h has an redefined class.
How to resolve this problem? Thanks!
Place a widget from toolbox and use promote and make sure your class structure is like following sample.
#ifndef PLOT_H
#define PLOT_H
#include <QObject>
#include <qwt_plot.h>
class Plot : public QwtPlot
{
Q_OBJECT
public:
explicit Plot(QWidget *parent = nullptr);
signals:
public slots:
};
#endif // PLOT_H

Creating a class derived from CTabCtrl

I am following a tutorial to use CTabCtrl. The tutorial is asking me to create a class that is derived from CTabCtrl. I am unsure if I am doing this correctly. This is the header for my custom class.
#pragma once
// CTabbyControl
class CTabbyControl : public CTabCtrl
{
DECLARE_DYNAMIC(CTabbyControl)
public:
CTabbyControl();
virtual ~CTabbyControl();
protected:
DECLARE_MESSAGE_MAP()
};
I am using Visual Studio 2010 and am following this tutorial.
My problem is that the tutorial asks me to create a CTabbyControl variable in the class wizard. I only have the option to use CTabCtrl. This brings me to think that I am deriving incorrectly. I have never derived a class. Is the way I am doing it a correct method of deriving?
http://simplesamples.info/MFC/CTabCtrl.html
Please check whether you have included the Header file (TabbyControl.h) in the class you try to create the object of TabbyControl.

Qt Extending my own widget

To put it simply, I want a new class that extends a custom widget that I've made, and thus have full access to it's UI.
I've tried several different methods so far based on how one would normally subclass/extend classes, but I'm somehow failing horribly for so many different reasons.
Additionally, simply using my widget as a member in my new class wouldn't do for this situation.
Can someone illustrate a quick example of how I would do this? I have done a bunch of searching but I can't seem to find any hits relating to exactly what I'm trying to do
If all else fails I will simply copy over the code and make an actual new widget, which technically would have saved me lots time, but it just doesn't feel right doing that.
My first instinct was to do something like this ( Qwe being my new class, Asd being the widget ):
class Qwe : Asd {public: ...}
And I even made the widget's Ui public, but then I just got the error :
use of undefine type Ui::Asd
whenever I tried to access the Ui's elements.
Let's say we have a custom widget named BaseWidget and a child widget named ChildWidget. Declare BaseWidget as usually, but make its ui member protected instead of private, like this:
protected:
Ui::BaseWidget *ui;
Declare ChildWidget as an ordinary widget derived from BaseWidget. Make sure you include ui_BaseWidget.h in the ChildWidget.cpp file, just as you do it in BaseWidget.cpp (this include and the header itself is generated by Qt).
Header:
#include "BaseWidget.h"
class ChildWidget : public BaseWidget {
Q_OBJECT
public:
explicit ChildWidget(QString text, QWidget *parent = 0);
};
Source:
#include "ChildWidget.h"
#include "ui_BaseWidget.h"
ChildWidget::ChildWidget(QString text, QWidget *parent) :
BaseWidget(parent)
{
ui->label->setText(text);
}

C++ Qt Derived Classes

I'm trying to learn Qt and C++ and having some trouble understanding the C++ this keywork. I've seen examples where a class is derived from QMainWindow and then within the class member functions, a QMenu is added. One example is the "Simple menu" program described on this page:
http://www.zetcode.com/gui/qt4/menusandtoolbars/
In that example, a quit action is created with
QAction *quit = new QAction("&Quit", this);
However, imagine I want to also derive a class from QMenu and use that to create my menu.
mymenu.h
class MainWindow; // forward declaration
class MyMenu : QMenuBar
{
public:
MyMenu(MainWindow *main_window);
};
mymenu.cpp
#include "mymenu.hpp"
MyMenu::MyMenu(MainWindow *main_window) : QMenuBar()
{
QAction *quit = new QAction("&Quit", main_window); // Notice here I replaced
// 'this' with 'main_window'
QMenu = *file;
file = menuBar()->addMenu("&File");
file->addAction(quit);
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
}
Unfortunately this doesn't work because QAction expects a QObject as a parent. All that being said, there are a couple things that don't make sense to me:
If the class MainWindow inherits from QMainWindow, doesn't that make 'MainWindow' a QObject?
What is the difference between passing 'this' to QAction from within the class MainWindow, as opposed to passing 'main_window' which is (as far as I can tell) also a pointer to the instance from within the MyMenu class?
I apologize for such a long winded question, but if any of you have made it to the end with me, I would love any suggestions as to what I am missing here. The end goal here is just to create a derived class of QMenu (MyMenu here) and add it to the QMainWindow derived class (MainWindow here) existing in a separate class. Thank you for your time.
If the class MainWindow inherits from QMainWindow, doesn't that make 'MainWindow' a QObject?
Yes, MainWindow is a QMainWindow which is a QObject (you can see this by browsing the inheritance tree on the API docs).
You have only forward declared MainWindow. Since the compiler does not have a definition for the class MainWindow it can only do miminal things with a pointer to MainWindow. In order for the compiler to "know" that MainWindow is a QMainWindow which is a QObject, you must provide a class definition for MainWindow. You can solve your compiler error with:
#include "MainWindow.h"
No dynamic cast is needed
Also, in Qt land to make something "really" a QObject you should put the Q_OBJECT macro on the object:
class MyMenu : QMenuBar
{
Q_OBJECT
public:
MyMenu(MainWindow *main_window);
};
It might save you a few headaches later on if you ever plan to use the object for signal/slots or other Qt stuff.
What is the difference between passing 'this' to QAction from within the class MainWindow, as opposed to passing 'main_window' which
is (as far as I can tell) also a pointer to the instance from within
the MyMenu class?
this is a pointer to your custom MyMenu class which is also a QMenuBar. main_window is a pointer to your custom MainMenu class which is also a a QMainMenu. So, two different objects in memory. The second argument of the QAction constructor takes a pointer to a parent widget. The parent widget is responsible for managing the memory of its children. Since it takes a QObject its reasonable to pass in either this or main_menu.
Also you should probably pass a parent to the QMenu constructor.
MyMenu::MyMenu(MainWindow *main_window) : QMenuBar(main_window)
This way MyMenu is correctly deleted when the MainWindow is deleted.
The usual Qt paradigm is:
MyMenu::MyMenu(<arg1>, <arg2>, ... QObject * parent) : QMenuBar(parent)
But in this case forwarding along main_window is good enough.

Qt problem when working over classes GUI access

I got 2 classes:
- MainWindow (Was the default class)
- ExtraClass (That i created myself)
Inside the class MainWindow i've made a public function called "logger". This function looks like this:
// Takes in a QString and appends it to a QTextEdit.
void MainWindow::logger(QString Log_MSG)
{
ui->Logg->append(Log_MSG);
}
This logger functions works out as expected inside its own Class MainWindow but when i try to pass in a MSG into logger from the class ExtraClass, it suddenly doesn't work.
My approach to accessing logger from MainWindow to ExtraClass:
MainWindow Con;
Con.logger("The Message the will get appended to ui->logg");
So the question, what have i missed? I don't get any errors and the text "Log_MSG" that should be appended to the QTextEdit Log don't execute.
Sorry for the style, i just don't understand how to get it to look good.
EDIT:
I've already tried to access other functions from "MainWindow class"
and that works but when i try to pass a string this particuallry function "logger"
from another class nothing happens.
For an instance:
MainWindow MainWindow;
int ANumber = MainWindow.GiveMeAValue(); // This works
But when i'm doing this:
MainWindow MainWindow;
MainWindow.logger("Log MSG"); // This dosen't work
My guess is that the problem lies in the appendment of
a QString passed in into the main class that was automatically created by Qt (have stuff like ui->abc) from another class. But in my current
level of understandment of Qt i don't really know where to
troubleshoot beocuse i don't even get an error.
Your code to access the logger is wrong (it shouldn't even compile).
First, everytime you call the function where this code resides, you create a new local MainWindow object (Con). And then you try to call the method on the class and not on an object. If it is a static method (which I doubt, due to the use of ui), you would have to write MainWindow::logger(). If it is not a static method, then you need to call it on a specific MainWindow instance. But instead of creating a local MainWindow everytime, you should provide the correct application's MainWindow instance to your ExtraClass object.
If all this sounds alien to you, you should first look a bit deeper into fundamental C++ programming before delving into Qt.