GUI library for D

Adam D. Ruppe destructionator at gmail.com
Mon Apr 4 20:07:16 PDT 2011


Nick Sabalausky wrote:
> Looking at the pages that are there for QtD, and the source
> browser, I'm honestly not sure how to even get started with it.

Somewhere, there was a binary distribution with the needed .libs..
I don't remember where, but I think I still have a copy on my
other laptop (not available right now though).

Note that the duic for Qt Designer files apparently is behind
some changes - it won't work right. Gotta write straight D yourself.

But, you install it however, the process on the site I think works
but it takes a little while.

Anyway, here's a hello world I just whipped up with some comments
to keep in mind - stuff that took me hours to figure out...

The compile line looks like this on Linux:
dmd hello.d -I/usr/local/include/d -L-L/usr/local/lib -L-lqtdgui -L-lqtdcore
-L-lcpp_core -L-lcpp_gui -L-lQtGui -L-lQtCore

Note it takes a few seconds to compile. Pretty slow for D.

It's similar on Windows, but since I don't have my win laptop
available right now I don't quite remember what it was exactly.

Anyway, the program:

// Qt's files are pulled in from the qt.gui or qt.core packages
// Seems to require a pretty long list of imports....
import qt.gui.QApplication;
import qt.gui.QMessageBox;
import qt.gui.QPushButton;
import qt.gui.QWidget;

int main(string[] args) {
    // main looks a lot like a C++ qt program, right down to
    // wanting scope classes so the destructors run in order

    scope app = new QApplication(args);

    scope mywindow = new MyWindow();
    mywindow.show();

    return app.exec();
}

class MyWindow : QWidget {
    this() {
         button = new QPushButton(this);
         setWindowTitle("Hello"); // methods are same as C++ but
                                  // thankfully they use D strings

         // signals and slots are connected by putting the signature
         // in quotes. No need for the SIGNAL or SLOT macro from C++
         // You leave the signal_ or slot_ off (see below)
         connect(button, "clicked", this, "sayHello");
    }

    // signals and slots use a naming convention instead of a label
    // like in C++. signals are declared: void signal_myName();
    // and here is a slot. When connecting, leave signal_ or slot_
    // off the string
    void slot_sayHello() {
         // the static call like in C++
         QMessageBox.information(null, "hello", "hello");
         this.close();
    }

    QPushButton button;

    // You must remember to mix this in for any class that uses
    // signals and slots to work, otherwise it will segfault at
    // runtime on the connect calls.

    // It's like the C++ Q_OBJECT macro, but while you'd normally
    // put the C++ macro at the top of the class, this mixin needs
    // to be at the bottom of the class or you'll hit forward
    // reference hell when compiling.

    mixin Q_OBJECT;
}


More information about the Digitalmars-d mailing list