<div>Thanks Mark, I did some things that you recommended:<br></div><div><br></div><div>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:</div>
<div><br></div><div>class Notebook(QtGui.QWidget):<br> def __init__(self,parent):<br> QtGui.QWidget.__init__(self,parent)<br></div><div>...</div><div>...</div><div> self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)<br>
self.new_cell_action=QtGui.QAction("new cell",self)<br> self.addAction(self.new_cell_action)<br> <br> self.connect(self.new_cell_action,QtCore.SIGNAL("triggered(bool)"),self.new_cell_slot)<br>
</div><div><br></div><div>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).</div><div>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.</div>
<div><br></div><div>This is a fragment of the main file:</div><div><br></div><div>class IpythonQt(QtGui.QMainWindow,Ui_Qtipython):</div><div> def __init__(self):<br> QtGui.QMainWindow.__init__(self)<br> self.setupUi(self)<br>
<br> self.scrollArea = QtGui.QScrollArea(self.centralwidget)<br> self.scrollArea.setWidgetResizable(True)<br> self.verticalLayout_2.addWidget(self.scrollArea)<br> self.nb=Notebook(self.scrollArea)<br>
</div><div> self.scrollArea.setWidget(self.nb)<br> self.nb.new_input_cell()<br></div><div><br></div><div>The code can be found at <a href="http://github.com/muzgash/ipython/tree/master/IPython/gui/qt/">http://github.com/muzgash/ipython/tree/master/IPython/gui/qt/</a></div>
<div><font color="#888888"><br><br>Best regards.<br>--<br><a href="http://he1.udea.edu.co/gweb" target="_blank">Gerardo Gutiérrez Gutiérrez</a><br>Physics student<br>Universidad de Antioquia<br>Computational physics and astrophysics group (<a href="http://urania.udea.edu.co/sites/sites.php" target="_blank">FACom</a>)<br>
Computational science and development branch(<a href="http://urania.udea.edu.co/sites/facom-dev/" target="_blank">FACom-dev</a>)<br>Usuario Linux #492295</font><br><br>
<br>
</div><div><br></div><br><div class="gmail_quote">On 6 June 2010 01:58, Mark Summerfield <span dir="ltr"><<a href="mailto:list@qtrac.plus.com">list@qtrac.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Gerardo,<br>
<br>
I'm not sure I can help but have a few comments...<br>
<div class="im"><br>
On 2010-06-06, Gerardo Gutierrez wrote:<br>
> Hi, I'm writing a frontend for ipython and I've a problem with a context<br>
> menu...<br>
> The frontend is based on mathematica so it has cells when you can write<br>
> code and execute it (not yet implemented)<br>
> So the cells are QPlainTextEdits which are embedded in a QWidget:<br>
><br>
> class Notebook(QtCore.QObject):<br>
> def __init__(self,parent):<br>
> QtCore.QObject.__init__(self)<br>
> self._container=QtGui.QWidget(parent)<br>
> self._nb_menu=NbContextMenu(self._container)<br>
<br>
</div>I don't really understand why Notebook doesn't inherit QWidget directly.<br>
<div class="im"><br>
><br>
self.connect(self._nb_menu.new_cell,QtCore.SIGNAL("triggered(bool)"),self.n<br>
> ew_cell_slot) self._container.mousePressEvent = self.nb_mouse_click_event<br>
><br>
> so the last line is for the context menu to popup con right click:<br>
><br>
> def nb_mouse_click_event(self,event):<br>
> if event.button()==QtCore.Qt.RightButton:<br>
> self._nb_menu.popup(event.globalPos())<br>
<br>
</div>Do you really need to implement such a low-level event handler?<br>
If Notebook inherits QWidget rather than QObject you can set its context<br>
menu policy either to Qt.ActionsContextMenu (in which case you can add<br>
the actions directly to the Notebook using addAction()), or to<br>
Qt.CustomContextMenu (in which case you connect the<br>
customContextmenuRequested() signal to a slot where you create the<br>
context menu yourself).<br>
<div class="im"><br>
> The NbContextMenu class is described here:<br>
><br>
> class NbContextMenu(QtGui.QMenu):<br>
> def __init__(self,parent):<br>
> QtGui.QMenu.__init__(self,parent)<br>
> self.set_actions()<br>
><br>
> def set_actions(self):<br>
> self.new_cell=QtGui.QAction("new cell",self)<br>
> self.addAction(self.new_cell)<br>
<br>
</div>I don't understand why you need this class. If Notebook is a QWidget you<br>
can use one of the techniques I mentioned above. But if it is a QObject<br>
then why not use a plain QMenu and use its addAction() method to add<br>
actions to it directly?<br>
<div class="im"><br>
> The connection showed above calls a slot which calls a function to create<br>
a<br>
> new cell in the QWidget called _container:<br>
><br>
> def new_input_cell(self,index=0):<br>
> cell=InputCell(self._container)<br>
> if index==0:<br>
> prev_cell=self._cell_list[len(self._cell_list)-1]<br>
><br>
><br>
cell.setGeometry(QtCore.QRect(0,prev_cell.pos().y()+prev_cell.height()+5,39<br>
> 5,60)) self._cell_list.append(cell)<br>
> else:<br>
> prev_cell=self._cell_list[index-1]<br>
><br>
><br>
cell.setGeometry(QtCore.QRect(0,prev_cell.pos().y()+prev_cell.height()+5,39<br>
> 5,60)) self._cell_list.insert(index,cell)<br>
> cell.index=self._cell_list.index(cell)<br>
> self.connect(cell,QtCore.SIGNAL("cell_index"),self.active_cell_signal)<br>
><br>
><br>
self.connect(cell,QtCore.SIGNAL("blockCountChanged(int)"),self.expand_cell<br>
> )<br>
<br>
</div>Using hard-coded numbers will make the application fragile. You might<br>
get away with little tweaks like +5, but for the 39 and 60 it would be<br>
better to compute them, for example based on QApplication.globalStrut().<br>
<div class="im"><br>
> The problem is then, that through the QMenu QAction the cells are being<br>
> appended but not showed. This doesn't happen if I call the function<br>
> new_input_cell from the constructor in the mainwindow class.<br>
<br>
</div>I don't know why, but I am suspicious that you have made the QAction's<br>
parent be the QMenu; I would try making the QAction's parent the QWidget<br>
the QMenu is associated with (e.g., the Notebook if you change it to be<br>
a QWidget or self._container otherwise).<br>
<br>
Good luck!<br>
<div><div class="h5"><br>
--<br>
Mark Summerfield, Qtrac Ltd, <a href="http://www.qtrac.eu" target="_blank">www.qtrac.eu</a><br>
C++, Python, Qt, PyQt - training and consultancy<br>
"Advanced Qt Programming" - ISBN 0321635906<br>
</div></div></blockquote></div><br>