I have a 'nested', a 2-tiered qmenus in which I have created a context menu for renaming and deleting.
I created a subclass for the QMenus in hopes of making my code cleaner as I am unable to use eventFilter in my current code as it messes some functionalities..
For the renaming part, while it renames the first tiered, as soon as I tried to do the same for the second-tiered item, the prompt actually renames the first-tiered item.
The following is a code portion of the QMenu subclass that I did and if you do the following:
Right click and add in a new object called main
In main, create another object called sub
(This is an extra right-click) If you perform another right-mouse click on main and select rename options, and have it changed to 'newMain', this works
Perform the same action as detailed in point #3, but this time, rename on sub to newSub
If you open up the overall menu, noticed that newMain was changed to newSub, while sub remains unchanged.
Could someone kindly shed some light towards my QMenu subclass on where I have done it wrong?
import functools
import sys
from PyQt4 import QtGui, QtCore
class QAddAction(QtGui.QAction):
def __init__(self, icon=None, text="Add Item", parent=None):
if icon:
super(QAddAction, self).__init__(icon, text, parent)
else:
super(QAddAction, self).__init__(text, parent)
class QRenameAction(QtGui.QAction):
def __init__(self, icon=None, text="Rename Item", parent=None):
if icon:
super(QRenameAction, self).__init__(icon, text, parent)
else:
super(QRenameAction, self).__init__(text, parent)
class QDeleteAction(QtGui.QAction):
def __init__(self, icon=None, text="Delete Item", parent=None):
if icon:
super(QDeleteAction, self).__init__(icon, text, parent)
else:
super(QDeleteAction, self).__init__(text, parent)
class QCustomMenu(QtGui.QMenu):
"""Customized QMenu."""
def __init__(self, title, parent=None):
super(QCustomMenu, self).__init__(title=str(title), parent=parent)
self.setup_menu()
def setup_menu(self):
self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
def contextMenuEvent(self, event):
no_right_click = [QAddAction, QRenameAction, QDeleteAction]
if any([isinstance(self.actionAt(event.pos()), instance) for instance in no_right_click]):
return
self.show_adv_qmenu()
def show_adv_qmenu(self):
qmenu = QCustomMenu(self)
rename_menu_action = QRenameAction(text= "Rename Item", parent=self)
rename_slot = functools.partial(self.rename_menu_item)
rename_menu_action.triggered.connect(rename_slot)
qmenu.addAction(rename_menu_action)
delete_menu_action = QDeleteAction(text="Delete Item", parent=self)
delete_slot = functools.partial(self.delete_menu_item, delete_menu_action)
delete_menu_action.triggered.connect(delete_slot)
qmenu.addAction(delete_menu_action)
qmenu.exec_(QtGui.QCursor().pos())
def addAction(self, action):
super(QCustomMenu, self).addAction(action)
def rename_menu_item(self):
new_name, ok = QtGui.QInputDialog.getText(
self,
"Rename Menu Item ({0})".format(self.title()),
"New name:"
)
if ok:
self.setTitle(new_name)
def delete_menu_item(self, action):
reply = QtGui.QMessageBox.question(self, 'Message',
"Really Delete this item?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
parent = action.parent()
print parent
self.remove_menu_item(self)
else:
event.ignore()
def err_popup(self):
"""Prompts a notification popup."""
msg = QtGui.QMessageBox()
msg.setIcon(QtGui.QMessageBox.Critical)
msg.setText("Input name already exists. Please check.")
msg.setWindowTitle('Unable to add item')
msg.setStandardButtons(QtGui.QMessageBox.Ok)
msg.exec_()
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Context menu')
self.qmenu = QCustomMenu(title='', parent=self)
add_item_action = QtGui.QAction('Add new item', self,
triggered=self.add_new_item)
self.qmenu.addAction(add_item_action)
def contextMenuEvent(self, event):
action = self.qmenu.exec_(self.mapToGlobal(event.pos()))
def add_new_item(self):
main_menu_name, ok = QtGui.QInputDialog.getText(
self,
'Main Menu',
'Name of new Menu Item:'
)
if ok:
self._addMenuItemTest(main_menu_name)
def _addMenuItemTest(self, main_menu_name):
base_qmenu = QCustomMenu(title=main_menu_name, parent=self)
base_qmenu.setTearOffEnabled(True)
add_item_action = QAddAction(None, 'Add Item', base_qmenu)
slot = functools.partial(self.add_sub_item, base_qmenu)
add_item_action.triggered.connect(slot)
base_qmenu.addAction(add_item_action)
self.qmenu.addMenu(base_qmenu)
def add_sub_item(self, base_menu):
sub_menu_name, ok = QtGui.QInputDialog.getText(
self,
'Sub Menu',
'Name of new Sub Item:'
)
if ok:
action = QtGui.QAction(sub_menu_name, self)
slot = functools.partial(
self._callActionItem,
str(base_menu.title()),
str(sub_menu_name)
)
action.setCheckable(True)
action.setChecked(True)
action.toggled.connect(slot)
base_menu.addAction(action)
def _callActionItem(self):
pass
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Example()
window.show()
sys.exit(app.exec_())
Related
I have the following code to create a QTreeWidget and a contextmenu with 2 actions.
import sys
from PyQt5 import QtCore, QtWidgets
class Dialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__()
self.tw = QtWidgets.QTreeWidget()
self.tw.setHeaderLabels(['Name', 'Cost ($)'])
cg = QtWidgets.QTreeWidgetItem(['carrots', '0.99'])
c1 = QtWidgets.QTreeWidgetItem(['carrot', '0.33'])
self.tw.addTopLevelItem(cg)
self.tw.addTopLevelItem(c1)
self.tw.installEventFilter(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tw)
def eventFilter(self, source: QtWidgets.QTreeWidget, event):
if (event.type() == QtCore.QEvent.ContextMenu and
source is self.tw):
menu = QtWidgets.QMenu()
AAction = QtWidgets.QAction("AAAAA")
AAction.triggered.connect(lambda :self.a(source.itemAt(event.pos())))
BAction = QtWidgets.QAction("BBBBBB")
BAction.triggered.connect(lambda :self.b(source, event))
menu.addAction(AAction)
menu.addAction(BAction)
menu.exec_(event.globalPos())
return True
return super(Dialog, self).eventFilter(source, event)
def a(self, item):
if item is None:
return
print("A: {}".format([item.text(i) for i in range(self.tw.columnCount())]))
def b(self, source, event):
item = source.itemAt(event.pos())
if item is None:
return
print("B: {}".format([item.text(i) for i in range(source.columnCount())]))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Dialog()
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())
When opening the contextmenu in the header and clicking on one of the actions it prints either carrot or carrots, depending on where in the contextmenu I click. But I give the position of right click event to the functions.
So why is this happening and what can I do to stop it?
Your code has 2 errors:
The main error is that the itemAt() method uses the coordinates with respect to the viewport() and not with respect to the view (the QTreeWidget) so you will get incorrect items (the header occupies a space making the positions with respect to the QTreeWidget and the viewport() have an offset).
You should not block the eventloop, for example blocking the eventFilter you may be blocking other events that would cause errors that are difficult to debug, in this case it is better to use a signal.
class Dialog(QtWidgets.QDialog):
rightClicked = QtCore.pyqtSignal(QtCore.QPoint)
def __init__(self, parent=None):
super(Dialog, self).__init__()
self.tw = QtWidgets.QTreeWidget()
self.tw.setHeaderLabels(["Name", "Cost ($)"])
cg = QtWidgets.QTreeWidgetItem(["carrots", "0.99"])
c1 = QtWidgets.QTreeWidgetItem(["carrot", "0.33"])
self.tw.addTopLevelItem(cg)
self.tw.addTopLevelItem(c1)
self.tw.viewport().installEventFilter(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tw)
self.rightClicked.connect(self.handle_rightClicked)
def eventFilter(self, source: QtWidgets.QTreeWidget, event):
if event.type() == QtCore.QEvent.ContextMenu and source is self.tw.viewport():
self.rightClicked.emit(event.pos())
return True
return super(Dialog, self).eventFilter(source, event)
def handle_rightClicked(self, pos):
item = self.tw.itemAt(pos)
if item is None:
return
menu = QtWidgets.QMenu()
print_action = QtWidgets.QAction("Print")
print_action.triggered.connect(lambda checked, item=item: self.print_item(item))
menu.addAction(print_action)
menu.exec_(self.tw.viewport().mapToGlobal(pos))
def print_item(self, item):
if item is None:
return
texts = []
for i in range(item.columnCount()):
text = item.text(i)
texts.append(text)
print("B: {}".format(",".join(texts)))
Although it is unnecessary that you use an eventFilter to handle the contextmenu since a simpler solution is to set the contextMenuPolicy of the QTreeWidget to Qt::CustomContextMenu and use the customContextMenuRequested signal:
class Dialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__()
self.tw = QtWidgets.QTreeWidget()
self.tw.setHeaderLabels(["Name", "Cost ($)"])
cg = QtWidgets.QTreeWidgetItem(["carrots", "0.99"])
c1 = QtWidgets.QTreeWidgetItem(["carrot", "0.33"])
self.tw.addTopLevelItem(cg)
self.tw.addTopLevelItem(c1)
self.tw.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.tw.customContextMenuRequested.connect(self.handle_rightClicked)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tw)
def handle_rightClicked(self, pos):
item = self.tw.itemAt(pos)
if item is None:
return
menu = QtWidgets.QMenu()
print_action = QtWidgets.QAction("Print")
print_action.triggered.connect(lambda checked, item=item: self.print_item(item))
menu.addAction(print_action)
menu.exec_(self.tw.viewport().mapToGlobal(pos))
def print_item(self, item):
if item is None:
return
texts = []
for i in range(item.columnCount()):
text = item.text(i)
texts.append(text)
print("B: {}".format(",".join(texts)))
It seems by default the Tab method for changing focus through widgets only stops once in a QButtonGroup and one is expected to move inside the group with the arrow keys (which only works with radio buttons, anyway). I want, however, to create a button group with QCheckBox inside, with the "exclusive" behaviour but allowing to uncheck all boxes, and where I can use the Tab key to move normally, as if they were not in a group.
The "clearable" part I could get it done with a subclass, but for the tabbing, it seems as soon as a button in the group gets the focus, it changes the focusPolicy for all other buttons such that they don't accept Tab (from 11 to 10), while the button that got the focus changes to 11. How can I disable/override this? Who is messing with the focus policies? I've tried defining a focusInEvent method for a QCheckBox, and I see that it changes "this button's" focusPolicy, but how can I know, from "this button" what the "other buttons" are (given that the final application may have many button groups)? Ideally, I would do something to the QButtonGroup subclass, but I don't know if it has any method that can respond to a focus change in its buttons.
Here is a small example. The "Tab order" button prints the current tab order and focus policies:
#!/usr/bin/env python3
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize
class Custom(QWidget):
def __init__(self, text1, text2):
QWidget.__init__(self)
self.box = QCheckBox(text1)
self.button = QPushButton(text2)
layout = QHBoxLayout()
layout.addWidget(self.box)
layout.addWidget(self.button)
self.setLayout(layout)
self._text = f'{text1} {text2}'
self.setFocusPolicy(QtCore.Qt.ClickFocus)
def text(self):
return self._text
class ClearableButtonGroup(QButtonGroup):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.button = None
def addButton(self, button):
try:
super().addButton(button)
button.pressed.connect(self.button_pressed)
button.clicked.connect(self.button_clicked)
except TypeError:
pass
def removeButton(self, button):
try:
button.pressed.disconnect(self.button_pressed)
button.clicked.disconnect(self.button_clicked)
super().removeButton(button)
except AttributeError:
pass
def button_pressed(self):
if (self.sender() is self.checkedButton()):
self.button = self.sender()
else:
self.button = None
def button_clicked(self):
button = self.sender()
if (button is self.button):
exclusive = self.exclusive()
self.setExclusive(False)
button.setChecked(False)
self.setExclusive(exclusive)
class HelloWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(centralWidget)
self.box1 = Custom('Box 1', 'Button 1')
self.box2 = Custom('Box 2', 'Button 2')
self.box3 = Custom('Box 3', 'Button 3')
self.box4 = Custom('Box 4', 'Button 4')
gridLayout.addWidget(self.box1, 0, 0)
gridLayout.addWidget(self.box2, 1, 0)
gridLayout.addWidget(self.box3, 2, 0)
gridLayout.addWidget(self.box4, 3, 0)
button1 = QPushButton('Tab order')
gridLayout.addWidget(button1, 4, 1)
button1.clicked.connect(self.tab)
group = ClearableButtonGroup(self)
group.setExclusive(True)
group.addButton(self.box1.box)
group.addButton(self.box2.box)
group.addButton(self.box3.box)
group.addButton(self.box4.box)
def tab(self):
print_tab_order(self)
def print_tab_order(widget):
w = widget
while True:
try:
print('Text: {}; FocusPolicy: {}'.format(w.text(), w.focusPolicy()))
except AttributeError:
pass
w = w.nextInFocusChain()
if w == widget:
break
print('----')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )
It seems that this behavior is missing, if the QAbstractButton source code is revised it is implemented:
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qabstractbutton.cpp?h=5.14#n1088
void QAbstractButton::keyPressEvent(QKeyEvent *e)
{
// ...
d->moveFocus(e->key());
// ...
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qabstractbutton.cpp?h=5.14#n247
void QAbstractButtonPrivate::moveFocus(int key)
{
// ...
if (candidate) {
if (key == Qt::Key_Up || key == Qt::Key_Left)
candidate->setFocus(Qt::BacktabFocusReason);
else
candidate->setFocus(Qt::TabFocusReason);
}
}
As it is observed, all the buttons are considered as a unit and there is no way to disable it, so a workaround is to implement a class that has the same functionality, and in a previous post I implemented it which in your case is:
class ButtonManager(QtCore.QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._buttons = []
#property
def buttons(self):
return self._buttons
def add_button(self, button):
if isinstance(button, QtWidgets.QAbstractButton):
button.toggled.connect(self.on_toggled)
self.buttons.append(button)
#QtCore.pyqtSlot(bool)
def on_toggled(self, state):
button = self.sender()
if state:
for b in self.buttons:
if b != button and b.isChecked():
b.blockSignals(True)
b.setChecked(False)
b.blockSignals(False)
else:
button.blockSignals(True)
button.setChecked(False)
button.blockSignals(False)
# ...
button1.clicked.connect(self.tab)
button = ButtonManager(self)
button.add_button(self.box1.box)
button.add_button(self.box2.box)
button.add_button(self.box3.box)
button.add_button(self.box4.box)
# ...
Is there a way I can get the items being drag/dropped and their destination parent?
In an ideal scenario what I want to happen is once the dropEvent finishes, it prints the qtreewidgetitems which were moved, as well as the new parent which the items were moved to. The parent would either be the qtreewidget itself or another qtreewidgetitem depending on where the drop happened.
Can someone help me out here please?
Below is the code i have so far.
# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore, QtSvg
class TreeNodeItem( QtGui.QTreeWidgetItem ):
def __init__( self, parent, name="" ):
super( TreeNodeItem, self ).__init__( parent )
self.setText( 0, name )
self.stuff = "Custom Names - " + str(name)
class TreeWidget(QtGui.QTreeWidget):
def __init__(self, parent=None):
QtGui.QTreeWidget.__init__(self, parent)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setItemsExpandable(True)
self.setAnimated(True)
self.setDragEnabled(True)
self.setDropIndicatorShown(True)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.setAlternatingRowColors(True)
# def dropEvent(self, event):
# print "finished"
def dropEvent(self, event):
return_val = super( TreeWidget, self ).dropEvent( event )
print ("Drop finished")
d = event.mimeData()
print d, event.source()
return return_val
# Main
# ------------------------------------------------------------------------------
class ExampleWidget(QtGui.QWidget):
def __init__(self,):
super(ExampleWidget, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(250, 400)
self.setWindowTitle("Example")
# widget - passes treewidget
self.itemList = QtGui.QTreeWidget()
self.itemList = TreeWidget()
headers = [ "Items" ]
self.itemList.setColumnCount( len(headers) )
self.itemList.setHeaderLabels( headers )
# layout Grid - row/column/verticalpan/horizontalspan
self.mainLayout = QtGui.QGridLayout(self)
self.mainLayout.setContentsMargins(5,5,5,5)
self.mainLayout.addWidget(self.itemList, 0,0,1,1)
# display
self.show()
# Functions
# --------------------------------------------------------------------------
def closeEvent(self, event):
print "closed"
def showEvent(self, event):
print "open"
for i in xrange(20):
TreeNodeItem( parent=self.itemList , name=str(i) )
# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = ExampleWidget()
sys.exit(app.exec_())
I would suggest using a View/Model approach.
Decoding the 'application/x-qabstractitemmodeldatalist' will only return the item dragged, and the dropMimeData isn't used in the QTreeWidget.
Look here for an example.
https://wiki.python.org/moin/PyQt/Handling%20Qt's%20internal%20item%20MIME%20type
I am working on set a click() event to QLineEdit, I already successfully did it. But I want to go back to Mainwindow when the QLine Edit is clicked because I need the data in Mainwindow to further process the data. But I failed to let it go back, neither nor to cite the Mainwindow as parent, I hope someone can point it out. Thank you so much.
MainWindow
{
...
self.tc = MyLineEdit(self.field[con.ConfigFields.VALUE])#self.tc = wx.TextCtrl(self.parent, -1, str(field[con.ConfigFields.VALUE]), pos=(x+220, y-3), size=(200, -1))
...
}
class MyLineEdit(QtGui.QLineEdit):
def __init__(self, parent=MainWindow):
super(MyLineEdit, self).__init__(parent)
#super(CustomQLineEidt, self).__init__()
def mousePressEvent(self, e):
self.mouseseleted()
def mouseseleted(self):
print "here"
MainWindow.mousePressEvent
I use the following to connect any method as the callback for a click event:
class ClickableLineEdit(QLineEdit):
clicked = pyqtSignal() # signal when the text entry is left clicked
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton: self.clicked.emit()
else: super().mousePressEvent(event)
To use:
textbox = ClickableLineEdit('Default text')
textbox.clicked.connect(someMethod)
Specifically for the op:
self.tc = ClickableLineEdit(self.field[con.ConfigFields.VALUE])
self.tc.clicked.connect(self.mouseseleted)
Just simply call the MainWindow mousePressEvent and give it the event variable the line edit received
class MyLineEdit(QtGui.QLineEdit):
def __init__(self, parent):
super(MyLineEdit, self).__init__(parent)
self.parentWindow = parent
def mousePressEvent(self, event):
print 'forwarding to the main window'
self.parentWindow.mousePressEvent(event)
Or you can connect a signal from the line edit
class MyLineEdit(QtGui.QLineEdit):
mousePressed = QtCore.pyqtProperty(QtGui.QMouseEvent)
def __init__(self, value):
super(MyLineEdit, self).__init__(value)
def mousePressEvent(self, event):
print 'forwarding to the main window'
self.mousePressed.emit(event)
Then just connect the signal in your main window where you created it
self.tc = MyLineEdit(self.field[con.ConfigFields.VALUE])#self.tc = wx.TextCtrl(self.parent, -1, str(field[con.ConfigFields.VALUE]), pos=(x+220, y-3), size=(200, -1))
self.tc.mousePressed[QtGui.QMouseEvent].connect(self.mousePressEvent)
This is what I used to do onClick for QLineEdits
class MyLineEdit(QtGui.QLineEdit):
def focusInEvent(self, e):
try:
self.CallBack(*self.CallBackArgs)
except AttributeError:
pass
super().focusInEvent(e)
def SetCallBack(self, callBack):
self.CallBack = callBack
self.IsCallBack = True
self.CallBackArgs = []
def SetCallBackArgs(self, args):
self.CallBackArgs = args
and in my MainGUI:
class MainGUI(..):
def __init__(...):
....
self.input = MyLineEdit()
self.input.SetCallBack(self.Test)
self.input.SetCallBackArgs(['value', 'test'])
...
def Test(self, value, test):
print('in Test', value, test)
I have a window with multiple tables using QTableWidget (PyQt). I created a popup menu using the right click mouse and it works fine.
However, I need to create different popup menu based on which table the mouse is hovering over at the time the right mouse is clicked. How can I get the mouse to tell me which table it is hovering over?
or, put in another way, how to implement a method so as to have a specific context menu based on mouse location?
I am using Python and PyQt.
My popup menu is developed similar to this code (PedroMorgan answer from Qt and context menu):
class Foo( QtGui.QWidget ):
def __init__(self):
QtGui.QWidget.__init__(self, None)
# Toolbar
toolbar = QtGui.QToolBar()
# Actions
self.actionAdd = toolbar.addAction("New", self.on_action_add)
self.actionEdit = toolbar.addAction("Edit", self.on_action_edit)
self.actionDelete = toolbar.addAction("Delete", self.on_action_delete)
# Tree
self.tree = QtGui.QTreeView()
self.tree.setContextMenuPolicy( Qt.CustomContextMenu )
self.connect(self.tree, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
# Popup Menu
self.popMenu = QtGui.QMenu( self )
self.popMenu.addAction( self.actionEdit )
self.popMenu.addAction( self.actionDelete )
self.popMenu.addSeparator()
self.popMenu.addAction( self.actionAdd )
def on_context_menu(self, point):
self.popMenu.exec_( self.tree.mapToGlobal(point) )
One way is to subclass QTableWidget and then implement your own contextMenuEvent method. Then you can set different handling of the context menu event for each of your instances. Here's a small example.
from PyQt4 import QtGui, QtCore
import sys
class MyTableWidget(QtGui.QTableWidget):
def __init__(self, name='Table1', parent=None):
super(MyTableWidget, self).__init__(parent)
self.name = name
def contextMenuEvent(self, event):
menu = QtGui.QMenu(self)
Action = menu.addAction("I am a " + self.name + " Action")
Action.triggered.connect(self.printName)
menu.exec_(event.globalPos())
def printName(self):
print "Action triggered from " + self.name
class Main(QtGui.QWidget):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.table1 = MyTableWidget(name='Table1', parent=self)
self.table2 = MyTableWidget(name='Table2', parent=self)
layout.addWidget(self.table1)
layout.addWidget(self.table2)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
app.exec_()