【问题标题】:Opening the text file in the textEdit in the pyQt在pyQt的textEdit中打开文本文件
【发布时间】:2014-04-03 11:22:38
【问题描述】:

我有 Qt 文件,用于通过单击按钮将文本文件读取到 textEdit,但是当我将其转换为 .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();
}

主窗口.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());
}

主窗口.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 文件,但上面的代码运行良好,请建议我在下面的代码中进行适当的更正。 主窗口.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_())

【问题讨论】:

    标签: python c++ qt pyqt pyuic


    【解决方案1】:

    你可以这样做:

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

    或者在你的代码中:

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

    您还可以在 PyQt here 中找到一个简单的文本编辑器。

    【讨论】:

      【解决方案2】:

      以下是您的 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

      这里有一个小小的好奇心。当自动connecting slots by name 时,可能需要使用pyqtSlot 装饰器来区分信号的不同重载。这种情况也发生在具有默认参数的信号(例如clicked)上,因为 PyQt 将这些实现为单独的重载(一个发送默认值,一个不发送任何内容)。如果不使用装饰器,both 重载将被连接,并且插槽将被调用两次。

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

          pyuic4 -o mainwindow.py mainwindow.ui
      

      【讨论】:

        猜你喜欢
        • 2014-03-18
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        相关资源
        最近更新 更多