Centering a window on the screen with Qt

September 10th, 2007 by exhuma.twn

Yet another thing that’s not automagic in Qt.
Here’s a python solution:

if __name__ == "__main__":

   app = QtGui.QApplication(sys.argv)

   dw = app.desktop().width()
   dh = app.desktop().height()

   myapp = MainWindow()
   myapp.setGeometry(
         int((dw - (dw - (dw / 2)) * 1.5) / 2),
         int((dh - (dh - (dh / 2)) * 1.5) / 2),
         int((dw - (dw / 2)) * 1.5),
         int((dh - (dh / 2)) * 1.5))
   myapp.show()
   sys.exit(app.exec_())

This will also resize the window, adapting to the desktop size.
Found and adapted from the Qt interest archive.

Posted in Python | No Comments »

Adding Scrollbars to a QFrame in Qt

September 10th, 2007 by exhuma.twn

Many things are very easy to accomplish in Qt. Qt’s designer is a great help with that. But the is no option to simply “enable” scrollbars for a frame. Also, the QScrollArea can nowhere be found in the designer. The solution is actually quite simple:

  1. Add the component you want to be scrollable (QFrame, QLabel, …) in the designer as usual
  2. In the application code:
    1. Create a new QScrollArea
    2. Set the parent of the widget you created in the designer to “None”
    3. Set the widget of the QScrollArea to the widget from step 1
    4. Insert the QScrollArea to you ui

And here’s some example code (boiled down to the essentials):

sa = QtGui.QScrollArea()
self.ui.myWidget.setParent(None)
sa.setWidget(self.ui.myWidget)
self.ui.vboxlayout1.insertWidget(0, sa)

You would probably write this somewhere in you application-constructor right after you call setupUi.

This will insert the scroll area at the top of vboxlayout1 (see the doc for insertWidget). You can of course also use addWidget.

It’s a shame that there’s no direct support for QScrollArea in designer. Maybe we will see it in some future release.

An example for C++ can be found in the Qt Forums.

Posted in Python | 2 Comments »

Deploying PyQt applications on Windows

July 12th, 2007 by exhuma.twn

My preferred way to deploy python applications on Windows is to use py2exe.

py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.

If you run in trouble with sip, read on…
Read the rest of this entry »

Posted in Python | 1 Comment »

Writing Qt apps in python

July 2nd, 2007 by exhuma.twn

Writing Qt apps in Python with Qt3 worked. Somewhat. I never found the energy to get it all set up. Finally with Qt4 we have something that “just works”. At least on Ubuntu and Windows (haven’t tested anywhere else).

Yes, since version 4, trolltech finally decided to release Qt as well under the GPL. To most of you it’s all old news. But hey…. just felt like writing it down again 😉

The problems with SIP are finally gone. So you just install python, Qt4 and pyqt4 and off you go.

Read the rest of this entry »

Posted in Python | No Comments »

Runnning a twisted application as windows service

June 27th, 2007 by exhuma.twn

Currently I am a bit busy, so I will just dump the post I found on usenet. Eventually I will clean it up…

On Wed, 22 Oct 2003 12:24:42 -0600
"Justin Johnson" <justinjohnson@fastmail.fm> wrote:

> The general consensus on the mailing list archives seems to be that
> I'd need to setup my code to not require twistd but just run
> standalone.

Uh. *Maybe* that's the consensus, but it's not actually correct. You can
have a tap or tac run as a NT service.

Lets say you have  a script "server.py" that is runnable with "twistd
-y", you can do (and similar code will work for TAPs):
import sys, os
import win32serviceutil, win32service

class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""

    _svc_name_ = "MyService"
    _svc_display_name_ = "MyService server"

    def SvcDoRun(self):
        import server
        f = open(os.path.join(server.rootPath, "cyberhigh.log"), 'a')
        from twisted.python.log import startLogging
        from twisted.application.app import startApplication
        from twisted.internet import reactor
        startLogging(f)
        startApplication(server.application, 0)
        reactor.run()

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        from twisted.internet import reactor
        reactor.stop()

if __name__ == '__main__':</pre>
    win32serviceutil.HandleCommandLine(MyService)</pre>
--
Itamar Shtull-Trauring    http://itamarst.org/
Available for Python & Twisted consulting


Posted in Python | No Comments »

Next Entries »

Pages

Recent Posts

Categories

Links


Archives

Meta