I have been trying to no avail to load a ui file and insert it into a tabWidget as a new tab. I commented out a try that I thought would work, I was wrong. I have tried several different ways but I always seem to get the same error.

The original gui MainWindow is a qt ui file as well. If I just do a simple show the ui it does a flash and is gone. Otherwise the error I get is self is

Traceback (most recent call last):
  File "test5.py", line 19, in createInvoice
    createInv()
  File "test5.py", line 38, in createInv
    ui.tabWidget.addTab(self, invoice)
NameError: global name 'self' is not defined
#!/usr/bin/python

import sys

from PyQt4 import QtCore, QtGui, uic, QtSql

class DemoImpl(QtGui.QMainWindow):
	def __init__(self, *args):
		super(DemoImpl, self).__init__(*args)
		uic.loadUi('K:\QTProjects\pos\mainwindow.ui', self)

		self.btnLogin.clicked.connect(self.createConnection)
		self.btnCreateInvoice.clicked.connect(self.createInvoice)

	def createConnection(self):
		createConn()

	def createInvoice(self):
		createInv()


def createConn():
	db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
	db.setDatabaseName(':memory:')
	if not db.open():
		QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Unable to establish a database connection.\nThis example needs SQLite support. Please read the Qt SQL driver documentation for information how to build it.\n\nClick Cancel to exit."), QtGui.QMessageBox.Cancel)
		return False
	else:
		QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Database Connected"), QtGui.QMessageBox.Cancel)
		return True

def createInv():
	invoice = uic.loadUi('K:\QTProjects\pos\invoicetoolbox.ui')
	QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Loaded Invoice"), QtGui.qApp.tr("Loaded Invoice"), QtGui.QMessageBox.Ok)
	print invoice
#	ui.invoice.show()
#	ui.tabWidget.addTab(self, invoice)

if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	ui = DemoImpl()
	ui.show()
	sys.exit(app.exec_())

Your indention is incorrect and the function is not inside the class.

Edited 5 Years Ago by pyTony: n/a

Your indention is incorrect and the function is not inside the class.

Sorry Changed editors and the new editor inserts spaces instead of tabs. Anyway changed the indents so they are proper to keep the functions within the class and I get these error instead

Traceback (most recent call last):
  File "C:\python\test5.py", line 16, in createConnection
    self.createConn()
TypeError: createConn() takes no arguments (1 given)
Traceback (most recent call last):
  File "C:\python\test5.py", line 19, in createInvoice
    self.createInv()
TypeError: createInv() takes no arguments (1 given)

I changed the coding of the script to this

#!/usr/bin/python

import sys

from PyQt4 import QtCore, QtGui, uic, QtSql

class DemoImpl(QtGui.QMainWindow):
    def __init__(self, *args):
        super(DemoImpl, self).__init__(*args)
        uic.loadUi('K:\QTProjects\pos\mainwindow.ui', self)

        self.btnLogin.clicked.connect(self.createConnection)
        self.btnCreateInvoice.clicked.connect(self.createInvoice)

    def createConnection(self):
       self.createConn()

    def createInvoice(self):
       self.createInv()

    def createConn():
        db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName(':memory:')
        if not db.open():
            QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Unable to establish a database connection.\nThis example needs SQLite support. Please read the Qt SQL driver documentation for information how to build it.\n\nClick Cancel to exit."), QtGui.QMessageBox.Cancel)
            return False
        else:
            QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Database Connected"), QtGui.QMessageBox.Cancel)
            return True

    def createInv():
        invoice = uic.loadUi('K:\QTProjects\pos\invoicetoolbox.ui')
        QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Loaded Invoice"), QtGui.qApp.tr("Loaded Invoice"), QtGui.QMessageBox.Ok)
        ui.tabWidget.addTab(self, invoice)
#	ui.invoice.show()
#	QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Invoice Being Created"), QtGui.qApp.tr("Invoice being Created"), QtGui.QMessageBox.Ok)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ui = DemoImpl()
    ui.show()
    sys.exit(app.exec_())

You should eliminate the pass-through functions.

class DemoImpl(QtGui.QMainWindow):
    def __init__(self, *args):
        super(DemoImpl, self).__init__(*args)
        uic.loadUi('K:\QTProjects\pos\mainwindow.ui', self)
        self.btnLogin.clicked.connect(self.createConnection)

    def createConnection(self):
        db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName(':memory:')
        if not db.open():
            QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Unable to establish a database connection.\nThis example needs SQLite support. Please read the Qt SQL driver documentation for information how to build it.\n\nClick Cancel to exit."), QtGui.QMessageBox.Cancel)
            return False
        else:
            QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Database Connected"), QtGui.QMessageBox.Cancel)
            return True

Edited 5 Years Ago by woooee: n/a

Thanks woooee and Tonyjv you help with the connections was excellent,

Now my 2 functions work as far as getting to them so how do I access the widget (I used the default name as there was only going to be 1 tab widget) named tabWidget so that I can add a new tab and then load the invoice into it?

This question has already been answered. Start a new discussion instead.