r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

How-To: Use Qt custom slots & generate Mocs in Visual Studio 2010

r3dux | April 4, 2012

A while back I had a play with Qt in Linux and wrote-up how to auto-generate MOCs with a bash script… This time I needed to get Qt working in Visual Studio 2010, on machines which get wiped every time they boot, so I needed a standalone “base-project” with minimal installation requirements for students to work from. So I made one…

Simple Qt Text Editor screenshot

Project Properties

Linked below is a Visual Studio 2010 project which is an incredibly bare-bones text editor in Qt. You can enter text, you can save it (to a fixed filename), and you can quit! That’s it!

But the project does have some redeeming features in that:

  • It’s completely standalone, because it comes with the required Qt headers, libs and dlls to do its thing, and
  • It has a batch file that runs as a pre-build step which auto-generates the MOC files (using an included copy of moc.exe) – meaning you can change any headers in the same location as the main source and it’ll update the MOC(s) for you on build without having to dip into the commandline.

Download: 1-Standalone-Qt-Project.zip

Please note, this download is for Windows 7 64-bit, and it includes the libs/headers/dlls/moc.exe from Qt 4.8.0 (the most recent at time of writing).

If you just want the batch file to autogen the MOCs for you, then the script itself is simply:

@ECHO OFF
FOR %%f IN (*.h) DO moc.exe %%f -o moc_%%~nf.cpp

Place the batch file in the same location as your code, which is generally in the named project folder – so if I had a project called 2-QtTest, my code (and the batch file, and moc.exe) would live in 2-QtTest\2-QtTest. Finally, add executing the batch file as pre-build step like this (or just grab my project and use it as a base):

Visual Studio Qt Moc Prebuild-step settings

Additional

  • If you want a ton of Qt functionality you might need to copy more of the Qt headers into the include folder in the project (if you have the QtSDK installed, you can find these in C:\QtSDK\Desktop\Qt\4.8.0\msvc2010\include). I only put the QtCore and QtGui folders in to keep the project size down.

  • If you get a warning about being unable to delete a temporary file during build (generated by the genQtMoc batch file) – it could well be caused by AVG. For example:
    PreBuildEvent:
      Description: Generating Qt Mocs
    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(103,5): warning MSB5018: Failed to delete the temporary file "C:\Users\r3dux\AppData\Local\Temp\547660b1862d4d3491ed0d06e65f4a7a.exec.cmd". The process cannot access the file 'C:\Users\r3dux\AppData\Local\Temp\547660b1862d4d3491ed0d06e65f4a7a.exec.cmd' because it is being used by another process.

    I’ve seen the error once or twice during build, but it’s not consistent – really, it’s just a tempfile being locked – shouldn’t be a concern. AVG finger-pointing source: karmiistha.

Source code after the jump for those who want it…

Read the rest of this entry »

Comments
No Comments »
Categories
Coding
Tags
Batch, Batchfile, C++, Macro, moc, Qt, Q_OBJECT, Visual Studio
Comments rss Comments rss
Trackback Trackback

PS3 XMB Wave in Flash / AS3

r3dux | April 2, 2012

My second attempt at recreating the PS3 XMB “wave” thingy in Flash – I’m thinking it’s not too shabby for purely using blurred 2D lines for the waves. I’m sure it could be fine-tuned to match up with the real XMB better, but for what I wanted this is fine.

And all using zero 3D, and zero splines/beziers.

Not bad for 7KB of Flash!

Comments
2 Comments »
Categories
Coding, Imagery
Tags
ActionScript, AS3, Flash, PS3, Wave, XMB
Comments rss Comments rss
Trackback Trackback

PS3 Wave in ActionScript 3 / Flash

r3dux | March 30, 2012

I had to cover a class on Flash today, and I haven’t touched it in over a year, so I got up bright and early to make sure I wasn’t too rusty…. and yeah… I was. Really rusty.

I was thinking in C++ and Python and Java, not thinking in AS3 and everything was wrong – so I decided to write a PS3 XMB (Cross Media Bar) “Wave-Thing” as practice. In fact, it doesn’t even do the waves – just the sparkles. Still, was a good exercise to get my head back in the game.

There’s nothing to stop me implementing the waves as fixed DisplayObjects moving in parallax (easy, weak), or via blurred splines (preferred, harder) – but at least I got the sparkles, um, sparkling…

I think I will do the waves, I need some spline practice…

Update: Modified so spark is allocated to MC with one of four levels of blur, increased spark density, spark has 50% chance of pulsing again without being re-initialised, spark has 1% chance of being big on initialisation.

Credits: PS3 wave background from: http://blog.guifx.com/2009/04/24/playstation-3-wallpapers/

Comments
No Comments »
Categories
Coding, Imagery
Tags
Flash, PS3, Sparkles, Sparks, Wave, XMB
Comments rss Comments rss
Trackback Trackback

Avoiding down-casts in C++

r3dux | January 31, 2012

If you have to down-cast objects from a base class to a derived class then there’s probably a design flaw in the class structure. It might all work but it’s going to be a brittle design.

// C++ FAQS by Cline & Lowom
// FAQ 162 - What is a down-cast?
// Answer: Trouble.
 
#include <iostream>
 
using namespace std;
 
class Asset
{
	public:
		virtual ~Asset() { }
		virtual bool isLiquidatable() const { return false; } // BAD-FORM: Capability query
};
 
class LiquidAsset : public Asset
{
	protected:
		int mValue;
 
	public:
		LiquidAsset(int value = 100) : mValue(value) { }
 
		int getValue() const { return mValue; }
 
		void setValue(int theValue) { mValue = theValue; }
 
		virtual bool isLiquidatable() const { return true; } // BAD-FORM: Capability query
};
 
// Userland function to liquidate an asset (if it can)
int tryToLiquidate(Asset &asset)
{
	int value;
 
	if ( asset.isLiquidatable() )                     // BAD-FORM: Finds code using code
	{
		value = ((LiquidAsset&)asset).getValue(); // BAD-FORM: Down-cast
 
		((LiquidAsset&)asset).setValue(0);        // BAD-FORM: Down-cast
 
		cout << "Liquidated $" << value << endl;
	}
	else
	{
		value = 0;
		cout << "Sorry, couldn't liquidate this asset.\n";
	}
 
	return value;
}
 
int main()
{
    Asset       a;
    LiquidAsset b;
 
    tryToLiquidate(a);
    tryToLiquidate(b);
 
    return 0;
}

A better way to accomplish this is to give the users of your code the right tools in the first place as member functions, rather than them having to cobble together their own routines in userland:

// C++ FAQS by Cline & Lowom
// FAQ 163 - What is an alternative to using down-casts?
// Answer: An if/down-cast pair can often be replaced by a virtual function call. The key
// insight is to move the -context- of the capability query from the user's code into the
// virtual function; don't just move the primitive query used in the user's if statements.
 
#include <iostream>
using namespace std;
 
class Asset
{
	public:
		virtual ~Asset() { }
 
		virtual int tryToLiquidate()
		{
			cout << "Sorry, couldn't liquidate this asset.\n";
			return 0;
		}
};
 
class LiquidAsset : public Asset
{
	protected:
		int mValue;
 
	public:
		LiquidAsset(int value = 100) : mValue(value) { }
 
		int getValue() const { return mValue; }
 
		void setValue(int theValue) { mValue = theValue; }
 
		virtual int tryToLiquidate()
		{
			int value = mValue;
 
			mValue = 0;
 
			cout << "Liquidated $" << value << endl;
 
			return value;
		}
};
 
int main()
{
    Asset       a;
    LiquidAsset b;
 
    a.tryToLiquidate();
    b.tryToLiquidate();
 
    return 0;
}

Much better =D

Comments
No Comments »
Categories
Coding
Tags
C++, Class, Design, Down-cast, Structure, Virtual Function
Comments rss Comments rss
Trackback Trackback

Derived class issues with arrays and casting in C++

r3dux | January 29, 2012

I’m working my way through C++ FAQs book by Cline and Lomow, and it’s excellent. There’s lots of issues going on with inheritance, arrays and casting that could be a real pain to deal with towards the end of system development, but that you can nip in the bud and make life easier for yourself. For example, just knowing that an array of objects of a Derived class is NOT a kind-of array of objects of the Base class can prevent you a lot of headaches…

// Book: C++ FAQs by Cline & Lomow
// FAQ 136 & 137 - Is array-of Derived a kind-of array-of Base?
// Answer: NO!
 
#include <iostream>
using namespace std;
 
class Base
{
	protected:
		int i;
 
	public:
		Base() : i(42*42)      { }
		virtual ~Base()        { }
		virtual void service() { cout << "Base::service() called.\n" << flush; }
};
 
class Derived : public Base
{
	protected:
		// Add some extra things so that an object of type Derived is a different
		// size to an object of type Base
		int j;
		float k;
		unsigned long l;
 
	public:
		Derived() : Base(), j(42*42*42) { }
		virtual void service()          { cout << "Derived::service() called.\n" << flush; }
};
 
// Userland function
void useSubscript(Base *b)
{
	cout << "b[0].service(): " << flush; b[0].service();
	cout << "b[1].service(): " << flush; b[1].service(); // BOOM! Segfault!
 
	// This fails because the size of Base and the size of Derived are different:
	// "The fundamental problem is that a pointer to the first of an array-of-things
	// has exactly the same type as a pointer to a single thing. This was inherited
	// from C."
	//
	// In essence, as we're passing in Base pointers, in this case Base moves from
	// element to element in 16 byte intervals, but we actually provided an array
	// of Derived objects, which have a size of 32 bytes each, so b[1] starts
	// at b[0]+16, which is really only half-way through our first Derived element d[0]!
	//
	// The way to do this properly is to use a templatised container class instead.
	// If you tried to pass Array<Derived> as Array<Base> this would be caught at compile time.
}
 
int main()
{
    Derived d[10];
 
    cout << "Base has a size of   : " << sizeof(Base)    << " bytes." << endl;         // 16 bytes
    cout << "Derived has a size of: " << sizeof(Derived) << " bytes." << endl << endl; // 32 bytes
 
    useSubscript(d);
 
    return 0;
}

Good to know – now I just have to keep it in mind when I’m coding!

Comments
No Comments »
Categories
Coding
Tags
Array, Base, C++, Class, Crash, Derived, Kind-Of, Segfault
Comments rss Comments rss
Trackback Trackback

« Previous Entries

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash GLSL Gnome Hack How-To install Jaunty Java Kinect Linkage Linux Mash-Up Microsoft Motion OpenGL Particle Problem PS3 Remix Retro script Slides Sound Systems Texture Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“In theory, theory and practice are the same. In practice, they’re not.”

 - Yogi Berra

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox