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

How To: Use MySQL Connector/C++ to Connect to a MySQL Database in Windows

r3dux | November 27, 2010

I got this working in Linux in about ten minutes, while the same thing in Windows took me closer to three hours… The difficulty I had was that I need to get this working on multiple machines which all get wiped and reset on each boot, so it had to be entirely stand-alone with everything necessary to build in the same project. And the difficulty with that is that Connector/C++ needs the boost library to compile, which I wasn’t too keen on involving. In the end I couldn’t find a way around it and just threw the entire boost library into the same directory as the project as well as the libmysql.lib/dll and mysqlcppconn.lib libraries.

So now it works. It’s a little ugly, and the project folder comes in at 62MB, but it works. Really it’s not all that bad, if you just wanted to build and deploy something you’d end up with a couple of hundred KB executable plus a few MB of dlls. I guess I could go in and strip out some parts of the boost library which aren’t being used. But to be honest it’s been a long day and I’m sick of fighting with it, so I’ll leave that as an exercise for the reader.

Reminder / Info

  • This code is tested and working on Windows XP 32-Bit.
  • The project (i.e. download, at bottom of post) is for Microsoft Visual Studio 2008 Express
  • The version of the libs/headers included are:
    • libmysql.lib from MySQL server v1.5.36 32-Bit
    • boost library v1.45.0
    • mysqlcppconn.lib/dll from Connector/C++ v1.1.0 32-Bit
  • I really, really hate having to work with Windows.

Anyhoo – here’s the junk…

// Standard C++ includes
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
 
// Include the Connector/C++ headers
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"
 
// Link to the Connector/C++ library
#pragma comment(lib, "mysqlcppconn.lib")
 
// Specify our connection target and credentials
const string server   = "tcp://127.0.0.1:3306";
const string username = "root";
const string password = ""; // No password - thanks, WAMP Server!
 
int main()
{
	sql::Driver     *driver; // Create a pointer to a MySQL driver object
        sql::Connection *dbConn; // Create a pointer to a database connection object
        sql::Statement  *stmt;   // Create a pointer to a Statement object to hold our SQL commands
        sql::ResultSet  *res;    // Create a pointer to a ResultSet object to hold the results of any queries we run
 
	// Try to get a driver to use to connect to our DBMS
	try
	{
		driver = get_driver_instance();
	}
	catch (sql::SQLException e)
	{
		cout << "Could not get a database driver. Error message: " << e.what() << endl;
		system("pause");
		exit(1);
	}
 
	// Try to connect to the DBMS server
	try
	{
		dbConn = driver->connect(server, username, password);
	}
	catch (sql::SQLException e)
	{
		cout << "Could not connect to database. Error message: " << e.what() << endl;
		system("pause");
		exit(1);
	}
 
	stmt = dbConn->createStatement(); // Specify which connection our SQL statement should be executed on
 
	// Try to query the database
	try
	{
		stmt->execute("USE mysql");              // Select which database to use. Notice that we use "execute" to perform a command.
 
		res = stmt->executeQuery("show tables"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back
	}
	catch (sql::SQLException e)
	{
		cout << "SQL error. Error message: " << e.what() << endl;
		system("pause");
		exit(1);
	}
 
	// While there are still results (i.e. rows/records) in our result set...
	while (res->next())
	{
		// ...get each field we want and output it to the screen
		// Note: The first field/column in our result-set is field 1 (one) and -NOT- field 0 (zero)
		// Also, if we know the name of the field then we can also get it directly by name by using:
		// res->getString("TheNameOfTheField");
		cout << res->getString(1) << endl;                
	}
 
	// Clean up after ourselves
	delete res;
	delete stmt;
	delete dbConn;
 
	system("pause");
	return 0;
}

*** REALLY IMPORTANT: Build the following project in Release mode if you want it to work!! No, really. The libs and dll’s that the project uses have been built in Release mode and you can’t mix n’ match. ***

Download of Visual Studio 2008 Express project including all necessary headers/libs/dlls (i.e. it’s ready to go – and 12MB): MySQL-Connector-Test.zip

Credits: Thanks to TidyTutorials for the guide that got me started.

Comments
26 Comments »
Categories
Coding
Tags
C++, Connector, MySQL, SQL, Visual Studio, WAMP, Windows
Comments rss Comments rss
Trackback Trackback

Translate

Categories

Archives

Tags

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

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“Let's organize this thing and take all the fun out of it.”

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