[PyQt] QPlainTextEdit not created in a widget through a QAction

Gerardo Gutierrez muzgash.lists at gmail.com
Mon Jun 7 14:52:46 BST 2010


Thanks Mark, I did some things that you recommended:

I changed the notebook class, so it inherits from QWIdget and no container
need to be used, the notebook itself is going to be embedded in the
QScrollArea:

class Notebook(QtGui.QWidget):
    def __init__(self,parent):
        QtGui.QWidget.__init__(self,parent)
...
...
        self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.new_cell_action=QtGui.QAction("new cell",self)
        self.addAction(self.new_cell_action)


self.connect(self.new_cell_action,QtCore.SIGNAL("triggered(bool)"),self.new_cell_slot)

The new_cell_slot calls the function to create the a new cell, which has not
changed since last mail (I'm not calculation yet the size of the cells).
The problem persists even if I make the QAction parent to be the notebook.
The cell are appended in the _cell_list attribute but not showed when the
QAction is calling the new_input_cell function.

This is a fragment of the main file:

class IpythonQt(QtGui.QMainWindow,Ui_Qtipython):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)

        self.scrollArea = QtGui.QScrollArea(self.centralwidget)
        self.scrollArea.setWidgetResizable(True)
        self.verticalLayout_2.addWidget(self.scrollArea)
        self.nb=Notebook(self.scrollArea)
        self.scrollArea.setWidget(self.nb)
        self.nb.new_input_cell()

The code can be found at
http://github.com/muzgash/ipython/tree/master/IPython/gui/qt/


Best regards.
--
Gerardo Gutiérrez Gutiérrez <http://he1.udea.edu.co/gweb>
Physics student
Universidad de Antioquia
Computational physics and astrophysics group
(FACom<http://urania.udea.edu.co/sites/sites.php>
)
Computational science and development
branch(FACom-dev<http://urania.udea.edu.co/sites/facom-dev/>
)
Usuario Linux #492295




On 6 June 2010 01:58, Mark Summerfield <list at qtrac.plus.com> wrote:

> Hi Gerardo,
>
> I'm not sure I can help but have a few comments...
>
> On 2010-06-06, Gerardo Gutierrez wrote:
> > Hi, I'm writing a frontend for ipython and I've a problem with a context
> > menu...
> > The frontend is based on mathematica so it has cells when you can write
> >  code and execute it (not yet implemented)
> > So the cells are QPlainTextEdits which are embedded in a QWidget:
> >
> > class Notebook(QtCore.QObject):
> >       def __init__(self,parent):
> >         QtCore.QObject.__init__(self)
> >         self._container=QtGui.QWidget(parent)
> >         self._nb_menu=NbContextMenu(self._container)
>
> I don't really understand why Notebook doesn't inherit QWidget directly.
>
> >
> self.connect(self._nb_menu.new_cell,QtCore.SIGNAL("triggered(bool)"),self.n
> > ew_cell_slot) self._container.mousePressEvent = self.nb_mouse_click_event
> >
> > so the last line is for the context menu to popup con right click:
> >
> >         def nb_mouse_click_event(self,event):
> >             if event.button()==QtCore.Qt.RightButton:
> >                 self._nb_menu.popup(event.globalPos())
>
> Do you really need to implement such a low-level event handler?
> If Notebook inherits QWidget rather than QObject you can set its context
> menu policy either to Qt.ActionsContextMenu (in which case you can add
> the actions directly to the Notebook using addAction()), or to
> Qt.CustomContextMenu (in which case you connect the
> customContextmenuRequested() signal to a slot where you create the
> context menu yourself).
>
> > The NbContextMenu class is described here:
> >
> > class NbContextMenu(QtGui.QMenu):
> >     def __init__(self,parent):
> >         QtGui.QMenu.__init__(self,parent)
> >         self.set_actions()
> >
> >         def set_actions(self):
> >             self.new_cell=QtGui.QAction("new cell",self)
> >             self.addAction(self.new_cell)
>
> I don't understand why you need this class. If Notebook is a QWidget you
> can use one of the techniques I mentioned above. But if it is a QObject
> then why not use a plain QMenu and use its addAction() method to add
> actions to it directly?
>
> > The connection showed above calls a slot which calls a function to create
> a
> > new cell in the QWidget called _container:
> >
> > def new_input_cell(self,index=0):
> >     cell=InputCell(self._container)
> >     if index==0:
> >         prev_cell=self._cell_list[len(self._cell_list)-1]
> >
> >
> cell.setGeometry(QtCore.QRect(0,prev_cell.pos().y()+prev_cell.height()+5,39
> > 5,60)) self._cell_list.append(cell)
> >     else:
> >         prev_cell=self._cell_list[index-1]
> >
> >
> cell.setGeometry(QtCore.QRect(0,prev_cell.pos().y()+prev_cell.height()+5,39
> > 5,60)) self._cell_list.insert(index,cell)
> >    cell.index=self._cell_list.index(cell)
> >    self.connect(cell,QtCore.SIGNAL("cell_index"),self.active_cell_signal)
> >
> >
> self.connect(cell,QtCore.SIGNAL("blockCountChanged(int)"),self.expand_cell
> > )
>
> Using hard-coded numbers will make the application fragile. You might
> get away with little tweaks like +5, but for the 39 and 60 it would be
> better to compute them, for example based on QApplication.globalStrut().
>
> > The problem is then, that through the QMenu QAction the cells are being
> > appended but not showed. This doesn't happen if I call the function
> > new_input_cell from the constructor in the mainwindow class.
>
> I don't know why, but I am suspicious that you have made the QAction's
> parent be the QMenu; I would try making the QAction's parent the QWidget
> the QMenu is associated with (e.g., the Notebook if you change it to be
> a QWidget or self._container otherwise).
>
> Good luck!
>
> --
> Mark Summerfield, Qtrac Ltd, www.qtrac.eu
>    C++, Python, Qt, PyQt - training and consultancy
>        "Advanced Qt Programming" - ISBN 0321635906
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100607/4f025f43/attachment.html>


More information about the PyQt mailing list