std::async, and copies – gotcha!

Disclaimer: I love C++11! It’s the best thing that happened to C++ since I’m using it (mid-90’s). I’ve been hoping for lambda‘s in C++ since I discovered and started using Lisp. Elisions and move semantics – together with unique and shared pointers – pretty much free you from thinking about memory management! It’s just amazing! Check out “Going Native” talks on YouTube, incl. amazing talks by the likes of Stroustrup, Sutter, Lavavej, et al.

If you don’t want to read my rant, here’s the point – function objects are objects – they can get copied, unless you specify otherwise, so beware that you’re working on a copy, or pass std::ref.

Continue reading

Get MainWindow in Qt Widgets

Update 2017/04/27 (thanks to grholk!):

The proper way would not be to check the objectName, which can change at any time, but rather to check if the widget is an instance of QMainWindow. Take advantage of what Qt makes available by using a foreach.

QMainWindow* getMainWindow()
{
    foreach(QWidget *widget, qApp->topLevelWidgets())
        if (QMainWindow *mainWindow = qobject_cast(widget))
            return mainWindow;
    return NULL;
}

Old version:

Provided how much you can do with Qt Widgets, or Qt in general, it’s no surprise that such “specialized” function is not present (the QApplication::activeWindow() does not make it, as the window might not be active).

#include <QApplication>
#include <QDateTime>

QMainWindow* getMainWindow()
{
    QWidgetList widgets = qApp->topLevelWidgets();
    for (QWidgetList::iterator i = widgets.begin(); i != widgets.end(); ++i)
        if ((*i)->objectName() == "MainWindow")
            return (QMainWindow*) (*i);
    return NULL;
}

If, for whatever reason, you changed the “objectName” property of the main window, update the above if with this name.

Automatic fclose() in C++11

This post is more of a mocking than serious advice, provided that a) STL includes <fstream> header with std::ifstream and std::ofstream classes, that do the same without any additional effort, and b) mixing templates and libC is just plain weird in my book. Continue reading

Turn Your Box into a Dev Web Server

To turn your box into a dev web server is pretty simple, yet I’ve seen people struggling with this over and over again, so here’s a simple TODO list.

I won’t go over the likes of Apache installation, VirtualHost setup, .htaccess settings, etc. as you can find these online very easily, and might be specific to the needs of your project.

Most of the time I see people using localhost:8080 and a myriad of other ports to work on several sites locally; but you might as well work on http://www.myproject.com locally in just 2 simple steps:

  • Create a VirtualHost in Apache which goes by “the name” http://www.myproject.com, by including the following within the definition:
    <VirtualHost *:80>
        ServerName www.myproject.com
        DocumentRoot /var/www/myproject/
        # all the other options and definitions ...
    </VirtualHost>
  • “Register” the domain name locally – simply add the following line to your /etc/hosts file (on Windows, this file is typically located under [Windows]/System32/Drivers/etc):
    127.0.0.1    www.myproject.com

Now enable the VHost in Apache, restart Apache, and open http://www.myproject.com in the browser, and you’re ready to code off!

Note: The VHost name need not be a valid domain name – feel free to use myproject as ServerName and in /etc/hosts; just beware that some browsers will try to search for “myproject” instead of opening the local site. In such case, type in the whole http://myproject.