开发者

Copy strings from multiple lineEdit slots as variable to one textEdit slot in PyQt4

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.开发者_如何学Clabel_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_())


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.


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.. =^_^=

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜