tags:

views:

33

answers:

2

to be more crystal clear, here how the things might work.

in python, to create a variable, simply we use

var1 = raw_input('your name?') 

so that when using

print 'your name is ' +var1

it will print the string stored in var1.

the question is how to make that using Pyqt4? I have 3 lineEdit symbolize as name, age and gender and one textEdit to print strings from lineEdit into this textEdit. it's just like making a phonebook. is it possible? how the code will look like? i'm eagerly to know the answer..

here i provide some source to make things clearer.

this is the ui:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'phonebook.ui'
#
# Created: Sat Oct  2 15:18:52 2010
#      by: PyQt4 UI code generator 4.7.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_phonebook(object):
    def setupUi(self, phonebook):
        phonebook.setObjectName("phonebook")
        phonebook.resize(240, 300)
        phonebook.setMinimumSize(QtCore.QSize(240, 300))
        phonebook.setMaximumSize(QtCore.QSize(240, 300))
        self.label = QtGui.QLabel(phonebook)
        self.label.setGeometry(QtCore.QRect(20, 30, 57, 14))
        self.label.setObjectName("label")
        self.label_2 = QtGui.QLabel(phonebook)
        self.label_2.setGeometry(QtCore.QRect(20, 60, 57, 14))
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtGui.QLineEdit(phonebook)
        self.lineEdit.setGeometry(QtCore.QRect(110, 30, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtGui.QLineEdit(phonebook)
        self.lineEdit_2.setGeometry(QtCore.QRect(110, 60, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.textEdit = QtGui.QTextEdit(phonebook)
        self.textEdit.setGeometry(QtCore.QRect(20, 142, 201, 141))
        self.textEdit.setObjectName("textEdit")
        self.pushButton = QtGui.QPushButton(phonebook)
        self.pushButton.setGeometry(QtCore.QRect(70, 100, 89, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(phonebook)
        QtCore.QMetaObject.connectSlotsByName(phonebook)

    def retranslateUi(self, phonebook):
        phonebook.setWindowTitle(QtGui.QApplication.translate("phonebook", "Simple Phonebook", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("phonebook", "Name:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("phonebook", "E-mail:", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit.setText(QtGui.QApplication.translate("phonebook", "lineEdit", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_2.setText(QtGui.QApplication.translate("phonebook", "lineEdit_2", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("phonebook", "Preview", None, QtGui.QApplication.UnicodeUTF8))

this is main source:

#! /usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui
from phonebook import Ui_phonebook

class Main(QtGui.QDialog):
  def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_phonebook()
    self.ui.setupUi(self)
    self.center()

    QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.preview)

  def preview(self):
    lineEdit = '"content inside lineEdit"'
    lineEdit_2 = '"content inside lineEdit_2"'
    self.ui.textEdit.setText('Name: ' +lineEdit +'\n\nEmail: ' +lineEdit_2)

  def center(self):
    screen = QtGui.QDesktopWidget().screenGeometry()
    size =  self.geometry()
    self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)


if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  main = Main()
  main.show()
  sys.exit(app.exec_())
A: 

When a push-button is clicked in Qt (and PyQt) it emits a signal. You can connect this signal to any slot (in PyQt that would be any Python method) and do whatever you wish in that slot - like look at text from 3 boxes and print something.

For example suppose you have a button you created with:

    self.start_button = QPushButton("&Start")

Connect its clicked signal:

    self.connect(self.start_button, SIGNAL('clicked()'), self.on_start)

to the on_start method of your class. In this method, invoke the relevant methods of other widgets to do whatever you wish.

Eli Bendersky
umm.. i have done those in my original source code.. i want the gui works just like example in my question..
nEk0
@nEk0: you want too much, then :-/
Eli Bendersky
aww.. no solution then??
nEk0
@nEk0: If you came for ready made solutions, it's the wrong place. Try rentacoder or something similar. In SO, you get directions, and you must invest effort of your own. With what I've explained in my answer your *solution* is just a few glimpses at the documentation and several synapse firings away. Good luck
Eli Bendersky
i think we have a bit of misunderstading here.. i have already made the application.. i've provide the full source code in my question above.. i just want a clue on how to make things work in that application.. have you take some time to test my code?? please.. i just want some clue..
nEk0
@nEk0: now read my answer again - it's more than a clue. Also, note how you got no other answers - you should think what this says about the quality of the question. I will have nothing more to say on this issue.
Eli Bendersky
i'm sorry about my bad attitude.. i've solved my own problem.. please forgive me.. my bad..
nEk0
A: 

i have solved this mysteries.. well, all i have to insert is text()!!

def preview(self):
  lineEdit = self.ui.lineEdit.text()
  lineEdit_2 = self.ui.lineEdit_2.text()
  self.ui.textEdit.setText('Name: ' +lineEdit +'\n\nEmail: ' +lineEdit_2)

yes.. that should make my dreams come true.. thanks to all who put an efford to help me.. =^_^=

nEk0