Active Topics

 


Reply
Thread Tools
Posts: 278 | Thanked: 209 times | Joined on Dec 2009
#1
This question is to the coders......

I've looked online, but I couldn't find a guide that told me everything from start to finish....and there are gaps between the guides out there.....

Can someone please just give me 1 detailed example? just 1 simple one?

(For Now) I want an app with one button, one line edit.

This button will simply insert the text in the line into a column in a MySQL database.on a server, in the same network.

Once I see that done from start to finish. I can build on it.......

I have Python 2.6 and PyQT4 Designer Installed.....I made the UI....(attached)

But I'm COMPLETELY clueless of what to do with signals or slots....or whats the connection code........or How to compile it into a working program.....

Can someone please please please help me? please be as DETAILED as you can.....I'm clueless......

Attachment 8335
 
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#2
Just so we are on the same page, what target environment are we talking about ? Is this on your desktop, and supposed to run on a N900 later on, or ? The PyQt examples/demo package has plenty of detailed examples, though I might have cut some database examples from the N900 default install...
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc
 

The Following User Says Thank You to attila77 For This Useful Post:
Posts: 278 | Thanked: 209 times | Joined on Dec 2009
#3
Thanks for your reply attila77.

My ultimate aim is actually learning Python. To keep my interest peeked in this learning endeavor, I wanted to make things for my n900. (For windows, I just make things with Visiual Studio which is what I'm used to)

I am running the dev tools on Windows. The target Environment (for now) is the n900. The app i want to make should "ultimately" be able to communicate with a MySQL db, lets say on a windows 2003 server with WAMP/XAMP (on the same Network that the n900 is connected to lets say), or atleast a local .db (sqlite?).

Last edited by Corso85; 2010-03-24 at 09:48.
 
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#4
I see. The main thing to keep in mind is that this Python+Qt stuff does not quite work the way you might be used to in VS, so the example you are trying to make is one of the more complex ones, as it involves a whole slew of technologies, Python, Qt, Qt's database models, UI design, etc. It's nowhere as integrated as your VS experience was (which is both an advantage and a disadvantage).

I really really really (really) suggest you start out with a 'hello world' style plain Python tutorial first, and then slowly progress to Qt, Qt GUI apps, and then databases. Taking it all in at once (especially with data models and networking) is a mighty big bite. That said, here's how you do GUI SQL in PyQt (example taken straight from the pyqt examples package I mentioned earlier on):

Code:
import sys
from PyQt4 import QtCore, QtGui, QtSql

def createConnection():
    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.\n"
                              "This 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, QtGui.QMessageBox.NoButton)
        return False

    query = QtSql.QSqlQuery()
    query.exec_("create table person(id int primary key, "
                "firstname varchar(20), lastname varchar(20))")
    query.exec_("insert into person values(101, 'Danny', 'Young')")
    query.exec_("insert into person values(102, 'Christine', 'Holand')")
    query.exec_("insert into person values(103, 'Lars', 'Gordon')")
    query.exec_("insert into person values(104, 'Roberto', 'Robitaille')")
    query.exec_("insert into person values(105, 'Maria', 'Papadopoulos')")
    return True


def initializeModel(model):
    model.setTable("person")

    model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
    model.select()

    model.setHeaderData(0, QtCore.Qt.Horizontal,
                        QtCore.QVariant(QtCore.QObject.tr(model, "ID")))
    model.setHeaderData(1, QtCore.Qt.Horizontal,
                        QtCore.QVariant(QtCore.QObject.tr(model, "First name")))
    model.setHeaderData(2, QtCore.Qt.Horizontal,
                        QtCore.QVariant(QtCore.QObject.tr(model, "Last name")))


def createView( title, model ):
    view = QtGui.QTableView()
    view.setModel(model)
    view.setWindowTitle(title)
    return view


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)

    model = QtSql.QSqlTableModel()

    initializeModel(model)

    view1 = createView(model.tr("Table Model (View 1)"), model)
    view2 = createView(model.tr("Table Model (View 2)"), model)

    view1.show()
    view2.move(view1.x() + view1.width() + 20, view1.y())
    view2.show()

    sys.exit(app.exec_())
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc
 

The Following User Says Thank You to attila77 For This Useful Post:
Posts: 278 | Thanked: 209 times | Joined on Dec 2009
#5
I came across this example when I first started my research. It was doing too many things so I got a bit lost.....

I already did the Hello World. It's just a label. then you make the .ui into a .py and execute that.

What is hindering me is that no body goes from start to finish. All tutorials pick one part and completely ignore the other.

For instance, I have now a working .py that can make a sqlite .db file and insert stuff in it......just like the one you posted......

HOWEVER, the insert statements and views are static. what if I want the Insert to take a value from a lineEdit box? What will be the story of the slots/receive/sender then? Lets say I want the results to display inside a GUI i made, not to spontaneously pop up like the example code?

And every time I see the word "import"...I get a good feeling it will not work as something will be missing in my setup...........something i have to somehow "compile"....

hmmm. maybe i'm getting too old.......
 
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#6
Originally Posted by Corso85 View Post
I already did the Hello World. It's just a label. then you make the .ui into a .py and execute that.
To put things straight - the .ui files are platform and language independent UI elements/dialogs, *not* the actual applications. You do (almost) none of the coding in the designer itself, it's use is layouting and a more of WYSIWYG feel to the UI, but not quite the integrated way of creating apps as it was in VS.

HOWEVER, the insert statements and views are static. what if I want the Insert to take a value from a lineEdit box? What will be
Again, first things first. After the hello world, check signal handling.

the story of the slots/receive/sender then? Lets say I want the results to display inside a GUI i made, not to spontaneously pop up like the example code?
If you're using a custom element (or one not directly supported by Qt), the usual 'technique' is to create a placeholder element (use a simple QWidget) in QtDesigner. Then, in your code, when the time comes, you just replace it or make your Widget a child of the placeholder.

Check out this:
http://zetcode.com/tutorials/pyqt4/

A bit dated, but does guide you through the basic concepts. AFAICS it does not deal with Designer, but in reality, the Designer is just a front end that generates (the same) code, so it does not matter much.
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc
 
Reply


 
Forum Jump


All times are GMT. The time now is 05:04.