Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

perl + Qt, the easy way

by vkon (Curate)
on Dec 13, 2014 at 13:53 UTC ( #1110271=perlmeditation: print w/replies, xml ) Need Help??

here is a recently tried by me approach, which appears to be coolest thing since sliced bread.

Code speaks louder than words.
Here I go:

use Inline Python => <<'EOS'; from PyQt4 import QtCore, QtGui class Window(QtGui.QWidget): def __init__(self, parent=None): super(Window, self).__init__(parent) grid = QtGui.QGridLayout() grid.addWidget(self.createFirstExclusiveGroup(), 0, 0) grid.addWidget(self.createSecondExclusiveGroup(), 1, 0) grid.addWidget(self.createNonExclusiveGroup(), 0, 1) grid.addWidget(self.createPushButtonGroup(), 1, 1) self.setLayout(grid) self.setWindowTitle("Group Box") self.resize(480, 320) def createFirstExclusiveGroup(self): groupBox = QtGui.QGroupBox("Exclusive Radio Buttons") radio1 = QtGui.QRadioButton("&Radio button 1") radio2 = QtGui.QRadioButton("R&adio button 2") radio3 = QtGui.QRadioButton("Ra&dio button 3") radio1.setChecked(True) vbox = QtGui.QVBoxLayout() vbox.addWidget(radio1) vbox.addWidget(radio2) vbox.addWidget(radio3) vbox.addStretch(1) groupBox.setLayout(vbox) return groupBox def createSecondExclusiveGroup(self): groupBox = QtGui.QGroupBox("E&xclusive Radio Buttons") groupBox.setCheckable(True) groupBox.setChecked(False) radio1 = QtGui.QRadioButton("Rad&io button 1") radio2 = QtGui.QRadioButton("Radi&o button 2") radio3 = QtGui.QRadioButton("Radio &button 3") radio1.setChecked(True) checkBox = QtGui.QCheckBox("Ind&ependent checkbox") checkBox.setChecked(True) vbox = QtGui.QVBoxLayout() vbox.addWidget(radio1) vbox.addWidget(radio2) vbox.addWidget(radio3) vbox.addWidget(checkBox) vbox.addStretch(1) groupBox.setLayout(vbox) return groupBox def createNonExclusiveGroup(self): groupBox = QtGui.QGroupBox("Non-Exclusive Checkboxes") groupBox.setFlat(True) checkBox1 = QtGui.QCheckBox("&Checkbox 1") checkBox2 = QtGui.QCheckBox("C&heckbox 2") checkBox2.setChecked(True) tristateBox = QtGui.QCheckBox("Tri-&state button") tristateBox.setTristate(True) tristateBox.setCheckState(QtCore.Qt.PartiallyChecked) vbox = QtGui.QVBoxLayout() vbox.addWidget(checkBox1) vbox.addWidget(checkBox2) vbox.addWidget(tristateBox) vbox.addStretch(1) groupBox.setLayout(vbox) return groupBox def createPushButtonGroup(self): groupBox = QtGui.QGroupBox("&Push Buttons") groupBox.setCheckable(True) groupBox.setChecked(True) self.pushButton = QtGui.QPushButton("&Normal Button") toggleButton = QtGui.QPushButton("&Toggle Button") toggleButton.setCheckable(True) toggleButton.setChecked(True) flatButton = QtGui.QPushButton("&Flat Button") flatButton.setFlat(True) popupButton = QtGui.QPushButton("Pop&up Button") menu = QtGui.QMenu(self) menu.addAction("&First Item") menu.addAction("&Second Item") menu.addAction("&Third Item") menu.addAction("F&ourth Item") popupButton.setMenu(menu) newAction = menu.addAction("Submenu") subMenu = QtGui.QMenu("Popup Submenu", self) subMenu.addAction("Item 1") subMenu.addAction("Item 2") subMenu.addAction("Item 3") newAction.setMenu(subMenu) vbox = QtGui.QVBoxLayout() vbox.addWidget(self.pushButton) vbox.addWidget(toggleButton) vbox.addWidget(flatButton) vbox.addWidget(popupButton) vbox.addStretch(1) groupBox.setLayout(vbox) self.pushButton.clicked.connect(perl.qwerty); toggleButton.clicked.connect(perl.qwertyT); return groupBox app = QtGui.QApplication([]) def get_app(): return app; def init_clock_window(): clock = Window() return clock EOS my $app = get_app(); my $w = init_clock_window(); sub qwerty { print "eyeye!!!\n"; $w->{pushButton}->setText('['.$w->{pushButton}->text().']'); } sub qwertyT { print "foo bar fluffy\n"; } $app->exec_();

runs just fine, wow...
As for today - when there is no usable PerlQT on CPAN - this brings tons of possibilities.

Yep, python is a nice lib... Long live Inline::Python !

Replies are listed 'Best First'.
Re: perl + Qt, the easy way
by aitap (Curate) on Dec 13, 2014 at 21:29 UTC
    there is no usable PerlQT on CPAN
    But isn't there one in KDE sources? It looks both official and quite alive. And at least one major GNU/Linux distro ships it.
      Thanks for the pointer, btw, I haven't saw it, although I searched here on PM and on CPAN to no results...
      still not satisfied though

      The README file reads

      PerlQt version 0.96 =================== This module provides bindings to the Qt 4 libraries for Perl. PREREQUISITES Perl > 5. I've tested on Perl 5.8.6 and 5.10.0. Qt > 4.0. I've tested on Qt 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.3, and 4. +7.2. SmokeQt. Avaliable as part of the kdebindings package, or Alien::Smok +eQt on CPAN. cmake
      Not very inspiring...

      what about windows? Qt5? Why its not on CPAN??

      I said *usable*...

        I too was surprised to see Qt4 bindings in KDE sources instead of CPAN. Since KDE 5 was released this summer, but last update of Perl-Qt was in November and involved something about KDE4, I guess that this binding will be slowly dying, too. But at least it's usable for now, while Qt5 still deals with font issues (all fixed in 5.4) having reached third minor release: surely, but slowly.

        Speaking about usability, at least 480 people regularly use perl-qt4 it and 7124 have it installed for some reason, despite the absence of packages depending on libqtgui4-perl (pyqt4 is 20 times more popular, and there are approximately 97 packages depending on python-qt4 while not being a python library; pyqt5 is not so widely used as of now).

        Your point is proven, but, still, I think that installing Qt module from KDE bindings is a lesser mess than employing Python as Qt GUI "driver".

Re: perl + Qt, the easy way
by morgon (Priest) on Dec 13, 2014 at 22:26 UTC
    I really think this is (from a technical point of view) cool, but for the life of me I cannot see any sensible application of it.

    If I know enough Python (and I need to know enough Python to make your approach work) to put together the Qt-part of a program, why would I then want to use Perl for the rest?

    I'd just do it all the way Python rather than incurring the risk of having to debug a potential mess of under the hood glue-code should anything not work the way I expect it to work...

      > why would I then want to use Perl for the rest?


      ... but the (maybe naive) question should rather be how to use Python bindings from XS and to avoid the Python-middle-layer.

      The demonstrated Python code doesn't look semantically different to Perl's, so why can't an XS extension dock to it?

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      I have tons of my perl apps already, some of which are really efficient, so rewriting them to python will only made them worse...
      So my approach will not only save me from rewriting to python, but also will provide me a possibility to continue using strong parts of perl...

      yep.... :-|

        Follow up question, why Qt? There are other GUI tool kits that have usable Perl bindings.

Re: perl + Qt, the easy way
by chrisburel (Initiate) on Jan 09, 2015 at 23:52 UTC

    Hi! I'm the author of PerlQt4, and I think I can answer a few of your questions.

    Why is it not on CPAN?

    I tried to do this a while ago. But there were ownership issues that I was not able to get sorted out. The module filename is called PerlQt4. But internally it follows a lot of the module names as the Qt3 bindings, so there's code in the Qt namespace, Qt::internal namespace, etc, which is already owned by Ashley Winters or Germain Garand (iirc). I tried contacting them, and I think I even tried contacting the CPAN admins, to no effect. So I wasn't able to make an official release due to those issues, and gave up. This was a few years ago, so maybe it's time to try again.

    Is Windows supported?

    Yes. But, the build system depends on cmake, so cmake must be installed. I thought this would be clear from the error message:

    "Output from 'C:\strawberry180\perl\bin\perl.exe Makefile.PL': You must have cmake installed to compile PerlQt4. at Makefile.PL line + 54"

    Is Qt5 supported?

    Currently, no. PerlQt depends on a library called smoke, which is maintained by the KDE developers. The smoke library is what is responsible for parsing Qt's headers, and generating an interface that PerlQt can work with. It's a bit like PyQt's dependence on sip. The smoke library currently does not successfully parse Qt5's headers.

    Is the project dying?

    Probably. I don't have as much time to devote to it as I used to. But hearing that people actually want to use it is encouraging.

      Why not a Qt4 namespace and leave Qt as a defacto Qt3 namespace?
        That would've been a good idea, if I had known that it was going to be a problem before I wrote the code. But it's been released since 2009, and changing the module namespace would break any existing PerlQt4 code out there.
      you seriously rock!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1110271]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-09-20 08:04 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (120 votes). Check out past polls.