繁体   English   中英

在pyQt的textEdit中打开文本文件

[英]Opening the text file in the textEdit in the pyQt

通过单击按钮,我具有将文本文件读取到textEdit的Qt文件,但是当我将其转换为.py时,它无法正常工作。 我有以下代码:main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  return a.exec();
}

mainwindow.ccp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QFile>
#include<QTextStream>
#include<QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::on_pushButton_clicked()
{
  QFile file("filename.txt");

  if( !file.open( QIODevice::ReadOnly))
      QMessageBox::information(0, "info", file.errorString());

  QTextStream in( &file );

  ui->textEdit->setText(in.readAll());
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

private slots:
  void on_pushButton_clicked();

private:
  Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

我已经转换了无法正常工作的.py文件,但效果不佳,请在下面的代码中建议我正确更正。 mainwindow.py:

from PyQt4 import QtCore, QtGui

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(810, 424)
    self.centralWidget = QtGui.QWidget(MainWindow)
    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.pushButton = QtGui.QPushButton(self.centralWidget)
    self.pushButton.setGeometry(QtCore.QRect(340, 0, 111, 27))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.scrollArea = QtGui.QScrollArea(self.centralWidget)
    self.scrollArea.setGeometry(QtCore.QRect(10, 30, 791, 331))
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
    self.scrollAreaWidgetContents = QtGui.QWidget()
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 787, 327))
    self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
    self.textEdit = QtGui.QTextEdit(self.scrollAreaWidgetContents)
    self.textEdit.setGeometry(QtCore.QRect(0, 0, 791, 331))
    self.textEdit.setObjectName(_fromUtf8("textEdit"))
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    MainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(MainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 810, 23))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    MainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(MainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(MainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    MainWindow.setStatusBar(self.statusBar)

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

  def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Text", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  MainWindow = QtGui.QMainWindow()
  ui = Ui_MainWindow()
  ui.setupUi(MainWindow)
  MainWindow.show()
  sys.exit(app.exec_())

您可以这样做:

textEdit = QPlainTextEdit()
text=open('file.txt').read()
textEdit.setPlainText(text)

或在您的代码中:

text=open('file.txt').read()
self.textEdit.setText(text)

您还可以找到在PyQt的简单的文本编辑在这里

以下是您的C ++代码的python端口。 我已经更改了一些变量名,但是在功能上完全相同。 应该将其保存在与mainwindow.py模块相同的目录中。

from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    @QtCore.pyqtSlot()
    def on_pushButton_clicked(self):
        file = QtCore.QFile('filename.txt')
        if not file.open(QtCore.QIODevice.ReadOnly):
            QtGui.QMessageBox.information(None, 'info', file.errorString())
        stream = QtCore.QTextStream(file)
        self.ui.textEdit.setText(stream.readAll())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

PS

这里有一个小的好奇心。 通过名称自动连接插槽时 ,可能有必要使用pyqtSlot装饰器来区分信号的不同过载。 带有默认参数的信号(例如clicked )也会发生这种情况,因为PyQt将它们作为单独的重载实现(一个发送默认值,一个不发送任何东西)。 如果不使用装饰器,则两个重载都将连接,并且插槽将被调用两次。

最后一点:通常不需要使用-x--execute标志运行pyuic 以下内容就足够了:

    pyuic4 -o mainwindow.py mainwindow.ui

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM