Ruby + Qt4、シンプルなテキストエディター、パート2

以前の投稿( f3ex.habrahabr.ru/blog/54673 )で、rubyとQt4を使用して「シンプルテキストエディター」を作成する方法を書きました。

Python + Qt4からRuby + Qt4への移植例の継続



パート2



次に、次の質問について考えてみましょう。ファイルが選択されていない場合、またはテキストに変更が加えられていない場合、「保存」ボタンは無効にする必要があります。

Qt Disignerの「enabled」プロパティはアクティビティを担当します。または、このプロパティは、setEnabledメソッドを使用してルビーコードから設定できます。

b_saveボタンを設定してfalseプロパティを有効にし(ボックスをオフにします)、editor.rbフォームを再生成します。



画像







テキストが変更されたことを確認するには、textEditにtextChanged()シグナルを使用します。



start.rbコードを次のように変更しましょう。



Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  1. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  2. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  3. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  4. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  5. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  6. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  7. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  8. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  9. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  10. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  11. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  12. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  13. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  14. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  15. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  16. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  17. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  18. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  19. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  20. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  21. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  22. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  23. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  24. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  25. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  26. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  27. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  28. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  29. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  30. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  31. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  32. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  33. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  34. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  35. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  36. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  37. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  38. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end



  39. Copy Source | Copy HTML require 'Qt4' require 'editor.rb' class StartQT4 < Qt::MainWindow slots 'file_dialog()' , 'file_save()' , 'enable_save()' def initialize parent= nil super @ui = Ui_Notepad.new @ui.setupUi self Qt:: Object .connect(@ui.b_open, SIGNAL( 'clicked()' ), self , SLOT( 'file_dialog()' )) Qt:: Object .connect(@ui.b_save, SIGNAL( 'clicked()' ), self , SLOT( 'file_save()' )) Qt:: Object .connect(@ui.editor_window, SIGNAL( 'textChanged()' ), self , SLOT( 'enable_save()' )) end def file_dialog f = Qt::FileDialog if @filename = f.getOpenFileName text = File . new (@filename).read @ui.editor_window.setText text # # "" textChanged() @ui.b_save.setEnabled false end end def enable_save @ui.b_save.setEnabled true end def file_save if @filename f = File .new @filename, 'w' f. puts @ui.editor_window.toPlainText f.close end end end if $ 0 == __FILE__ app = Qt::Application. new (ARGV) myapp = StartQT4 .new myapp.show app. exec end







追加されたもの。 textEditでテキストが変更され、ボタンをアクティブにするときに呼び出されるスロットを追加しました。

Copy Source | Copy HTML



  1. Qt :: Object .connect(@ ui.editor_window、SIGNAL( 'textChanged()' )、 self 、SLOT( 'enable_save()' ))




Copy Source | Copy HTML



  1. def enable_save
  2. @ ui.b_save.setEnabled true
  3. 終わり




file_dialogメソッドで、ファイルからtextEditにテキストを追加すると、ボタンが非アクティブになります。

Copy Source | Copy HTML



  1. @ ui.b_save.setEnabled false




ファイルを開くと、現在開いているファイルの変更は保存されません。 ダイアログボックス(メッセージボックス)を作成する必要があります。このダイアログボックスでは、「保存」、「保存しない」、「キャンセル」などの変更について尋ねられます。 これらの目的のために、MessageBoxを使用します。



メッセージボックス呼び出しは次のとおりです。

Copy Source | Copy HTML



  1. メッセージ= Qt :: MessageBox.new
  2. メッセージ。 幹部




画像



このウィンドウを設定する必要があります。 ボタンとテキストを追加します。 しかし、これをどのように行うのでしょうか? file_dialogメソッドを編集し、変更が行われた場合はメッセージを表示する必要があります。 変更があったかどうかはどのようにしてわかりますか? 「保存」ボタンがアクティブな場合(@ ui.b_save.idEnabled)-未保存の変更があります。 そして、変更を行います。



Copy Source | Copy HTML



  1. 「Qt4」が 必要
  2. 「editor.rb」が 必要
  3. クラス StartQT4 <Qt :: MainWindow
  4. スロット'file_dialog()''file_save()''enable_save()'
  5. def 初期化親= nil
  6. スーパー
  7. @ui = Ui_Notepad.new
  8. @ ui.setupUi self
  9. Qt :: Object .connect(@ ui.b_open、SIGNAL( ' clicked ()' )、 self 、SLOT( 'file_dialog()' ))
  10. Qt :: Object .connect(@ ui.b_save、SIGNAL( ' clicked ()' )、 self 、SLOT( 'file_save()' ))
  11. Qt :: Object .connect(@ ui.editor_window、SIGNAL( 'textChanged()' )、 self 、SLOT( 'enable_save()' ))
  12. 終わり
  13. def file_dialog
  14. 応答= false
  15. #ボタンテキスト:
  16. save = 'Save'
  17. discard = 「保存しない」
  18. キャンセル= 'キャンセル'
  19. #変更があった場合、message_boxを呼び出す
  20. if @ ui.b_save.isEnabled && @filename
  21. メッセージ= Qt :: MessageBox.new
  22. message.setText 「未保存の変更で何をすべきか?」
  23. message.setWindowTitle( 'メモ帳'
  24. message.setIcon(Qt :: MessageBox.Question)
  25. message.addButton(保存、Qt :: MessageBox.AcceptRole)
  26. message.addButton(破棄、Qt :: MessageBox.DestructiveRole)
  27. message.addButton(キャンセル、Qt :: MessageBox.RejectRole)
  28. message.setDetailedText( 「保存されていないファイルの変更:」 + @ filename.to_s)
  29. メッセージ。 幹部
  30. 応答= message.clickedButton.text
  31. #ファイルを保存する
  32. 応答==保存の場合
  33. file_save
  34. @ ui.b_save.setEnabled false
  35. 終わり
  36. #保存しない
  37. 応答==破棄の場合
  38. @ ui.b_save.setEnabled false
  39. 終わり
  40. 終わり
  41. #変更を保存したくない場合
  42. 応答==キャンセルしない限り
  43. f = Qt :: FileDialog
  44. if @filename = f.getOpenFileName
  45. text = Filenew (@filename).read
  46. @ ui.editor_window.setTextテキスト
  47. @ ui.b_save.setEnabled false
  48. 終わり
  49. 終わり
  50. 終わり
  51. def enable_save
  52. @ ui.b_save.setEnabled true
  53. 終わり
  54. def file_save
  55. @filenameの場合
  56. f = File .new @filename、 'w'
  57. f。 puts @ ui.editor_window.toPlainText
  58. f.close
  59. 終わり
  60. 終わり
  61. 終わり
  62. $ 0 == __FILE__の 場合
  63. app = Qt ::アプリケーション。 新規 (ARGV)
  64. myapp = StartQT4 .new
  65. myapp.show
  66. アプリ。 幹部
  67. 終わり




行18〜46に加えられた変更



Qt :: MessageBoxクラスの新しいインスタンスを作成し、メッセージテキスト(setText)、ウィンドウタイトルをsetWindowTitle、アイコン(setIcon)に割り当て、3つのボタンを設定します(「保存」、「保存しない」、「キャンセル」)。 2番目の引数として、ボタンのタイプ(ロール)を設定します。 これらのタイプに関する情報は、QTドキュメントに記載されています。 setDetailedTextは、「詳細の表示」ボタンをクリックしたときの詳細なメッセージ出力を設定します。 そして最後に、execメソッドを呼び出します。 message.clickedButton-押されたボタンのpushButtonタイプを返します。 この例では、このボタンのテキストを使用して操作します。

ダイアログボックスは次のようになります。



画像



サンプルのソースコードは、次の場所からダウンロードできます。

前の例: narod.ru/disk/6746280000/ruby_qt_simple_editor_1.zip.html

現在の例: narod.ru/disk/6746302000/ruby_qt_simple_editor_2.zip.html



次の例のルビーへの移植: www.rkblog.rk.edu.pl/w/p/extending-pyqt4-text-editor



All Articles