Creating a class derived from CTabCtrl - c++

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.

Related

Shortcut to create C++ class method?

I have class definition code with function to be crated name
class CAAA
{
public:
public_method
}
I expect to create function stub in header and .cpp with help of some shortcut.
Trying to create class method in fast way:
Shortcut ctrl+1 brings No suggestions available:
Shortcut ctrl+space brings No Default proposal:
How to solve this problem?

Is it possible to make QList<QAbstractListModel>?

I have a custom C++ class, called CPage, which inherits of QAbstractListModel.
class CPage : public QAbstractListModel
In another class called CSession, I want to have a set of CPage. For this purpose, CSession have a private attribute called Q_PageSet:
QList<CPage*> Q_PageSet;
My problem is: when I try to do the following, my program crashes:
Q_PageSet.size() or Q_PageSet.count()
So my question is: Is that because I am not allowed to make a QList of QAbstractListModel ?
Thanks.

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

Why Qt default project use separate header files for manwindow.cpp?

I just created a Qt default project with a Qt designer form.
The class MainWindow is declared in a mainwindow.h and then included in mainwindows.cpp.
Why is it done this way ? Why not a declaration of this form directly in mainwindows.cpp ?:
class MainWindow
{
...
}
What is the proper way to add my code ? For example, a button that trigger a method.
In C++ you typically put class definitions into header files (.h), and method implementations in source files (.cpp). That allows clients of the class to use the class without having to see the implementation of each function. That also means that when adding a method, you'll typically have to make two changes: add the method to the class definition (in the header) and then add the method's implementation to the .CPP file.
In header file:
class MainWindow
{
void SomeMethod();
};
In source file:
void MainWindow::SomeMethod()
{
// Your code here.
}
The definition of MainWindow class is needed in another file, where an instance of it is constructed in the main function and then shown. That's why the class needs to be defined in a header file.
There are a number of ways to add your own code: for the button you described you could create in entirely in the QtCreator UI, or you could create it "programmatically" in the MainWindow constructor.

Organization of code for a game using SDL

I've been using SDL for some days now, and I decided after following some tutorials to start developing my own clone of Galaga. However, I had some difficulty trying to find a proper layout for my code.
For example, I have a Spaceship class defined as follows:
class Spaceship : public Sprite
{
public:
Spaceship(SDL_Surface *surface);
Spaceship(const char *filename);
void handleEvent(SDL_Event *event);
};
where Sprite is a base class that holds the position on the screen and so on.
My constructor would be something like:
Spaceship::Spaceship(SDL_Surface *surface) :
Sprite(surface)
{
m_y = Game::screenHeight() - m_surface->h; //positions the ship at the bottom
}
From what I've seen it's not possible to use Game::screenWidth() [static class] because I'd need to include "game.h", which is basically the main game class and includes "spaceship.h", creating basically an infinite loop (I've tried using #ifndef etc. with no success).
Is it possible to achieve this kind of result?
EDIT: I found a way to overcome the problem (I just added the "game.h" include in the cpp file and not in the header file).
If you only want to store pointers or references to those objects, then you can forward-declare one or both of the classes with class Game; or class Spaceship;. This is also fine if they take these objects as parameters or return them (with some exceptions, afaik).
If you actually want both to have a member of the other, then this is not possible, as each object would then have a copy of itself inside it.
You need to break a cycle in your dependency graph.
For example, one can add a field to your Spaceship class which saves a screen height.