Ruby QT4 - Text Editor, Part 1
Prelude.
Somehow long ago it was necessary to write a GUI to the parser with Yandex dictionaries (translation). What is Kommersant? Of course Ruby. I thought then. But how surprised I was when I almost did not find Russian documentation and examples to get acquainted with GUI programming. And the English one didn’t help much: all are some short snippets and notes like hello_worlds. This refers to Qt. The Qt Disigner site still has an example for Qt3. I had to write GUY on GTK +, and then I rewashed - the documentation on the module’s website is incomplete, incomplete, without examples ...
Recently I needed to write GUI applications. But by this time, having become acquainted with the basis of Python - I decided to see how it relates to the GUI. And in the above privacy of Qt4, because I myself kederast :), and Qt4 is already replacing Qt3. In any case, on my desktop. And I was pleasantly surprised - that there are a lot of examples for Python, documentation, including and in Russian on this part. And I came across a assembly of good examples for Python + QT4 on the page: www.rkblog.rk.edu.pl/w/p/python - for example - writing a text editor from scratch. Where everything is detailed in the examples, everything is taken apart. Sat 2 pm, figured out the basis, and wrote his programmina.
And then I sit and think everything is fine in that example, and Python is almost the same Ruby. It is necessary to do a thing to Kommersant, to rewrite the examples in Russian and under Ruby. What actually do now.
And so, let's go.
Simple test editor. Part 1.
Now we will write a simple text editor (viewer for starters).
We will need ruby1.8 and rbuic4. Moreover, when installing the latter, the necessary libraries for working with Qt4 in ruby will have to be installed.
The interface will be done in Qt Designer (designer-qt4)
In Qt Designer we create a new form based on the “Main Window”.
We put 2 buttons of the “Push Button” type on the form and a field for editing text - “Text Edit”

For the first button, set the text - “Open File”: through the “text” property in the Property Editor window or by double-clicking on the button.
We also set ObjectName for it - “b_open”: again, through the Property Editor or in the context menu, select Change objectName.
For the main form, I set objectName as “notepad”, and for Text Edit as “editor_window”
For the second button, set the name to “close” and objectName to “b_close”. Button what should do? Right, close the window. In the QT-Disigner in the Signal / Slot Editor window, add a new slot:
Sender -> b_close
Signal -> clicked ()
Receive -> notepad
Slot -> close ()
And save the project as editor.ui
Now execute the command:
in order to get the Ruby class - Ui_Notepad.
The -x option adds instructions to the file to launch the application. You can now run the form to make sure everything works:
-Ku options are for correcting UTF-8.
But there is one subtlety - if we make any changes to the interface - the editor.rb file will be rewritten. Therefore, we will create a separate script that will call the form, call it start.rb.
We regenerate the form: rbuic4 editor.ui> editor.rb, and write start.rb as follows: Now the close button works. It's time to configure the "open" button. Make changes to start.rb If you start the application, when you click the ('clicked ()') button, the slot 'file_dialog ()' is called, and in turn the file_dialog method is called, which inserts the text 'aaaaaaaa' into the EditText. Now we use Qt :: FileDialog to select the file. The following code:
f.getOpenFileName will show a file selection dialog that closes after a file is selected (and the file path is returned), interrupting code execution. This works, but if we press “cancel” while the f.getOpenFileName window is working, we will not get the file path, but we will get the following error:
start.rb: 17: in `initialize ': can't convert nil into String (TypeError)

T .e. if the file is not selected, nil is returned. The following code includes checking the return value: Now we can select the file, but we cannot save it. Let's add the save button in QTDesign and assign objectName to 'b_save', and regenerate the GUI from the new ui file: Now the application looks like this:

We will add a slot for saving the file and create a connection to it with the signal “clicked ()” on the pushButton-button “save: And so we got an application that opens text files and saves the changes to this file. The next note will expand the features of the application. I ran the application on Ubuntu 8.10, there were no problems with the encoding, but do not forget about the ruby -Ku keys to support UTF-8. Z.Y. Sorry for the formatting of the code, the first time I write a post. Like tags , but everything moved to the left. I would be grateful if you tell me how to code the code privately. Z.Z.Y. I am still poorly versed in Qt terminology. I will also be glad if you comment on what a “connection”, “slot” is

Somehow long ago it was necessary to write a GUI to the parser with Yandex dictionaries (translation). What is Kommersant? Of course Ruby. I thought then. But how surprised I was when I almost did not find Russian documentation and examples to get acquainted with GUI programming. And the English one didn’t help much: all are some short snippets and notes like hello_worlds. This refers to Qt. The Qt Disigner site still has an example for Qt3. I had to write GUY on GTK +, and then I rewashed - the documentation on the module’s website is incomplete, incomplete, without examples ...
Recently I needed to write GUI applications. But by this time, having become acquainted with the basis of Python - I decided to see how it relates to the GUI. And in the above privacy of Qt4, because I myself kederast :), and Qt4 is already replacing Qt3. In any case, on my desktop. And I was pleasantly surprised - that there are a lot of examples for Python, documentation, including and in Russian on this part. And I came across a assembly of good examples for Python + QT4 on the page: www.rkblog.rk.edu.pl/w/p/python - for example - writing a text editor from scratch. Where everything is detailed in the examples, everything is taken apart. Sat 2 pm, figured out the basis, and wrote his programmina.
And then I sit and think everything is fine in that example, and Python is almost the same Ruby. It is necessary to do a thing to Kommersant, to rewrite the examples in Russian and under Ruby. What actually do now.
And so, let's go.
Simple test editor. Part 1.
Now we will write a simple text editor (viewer for starters).
We will need ruby1.8 and rbuic4. Moreover, when installing the latter, the necessary libraries for working with Qt4 in ruby will have to be installed.
The interface will be done in Qt Designer (designer-qt4)
In Qt Designer we create a new form based on the “Main Window”.
We put 2 buttons of the “Push Button” type on the form and a field for editing text - “Text Edit”

For the first button, set the text - “Open File”: through the “text” property in the Property Editor window or by double-clicking on the button.
We also set ObjectName for it - “b_open”: again, through the Property Editor or in the context menu, select Change objectName.
For the main form, I set objectName as “notepad”, and for Text Edit as “editor_window”
For the second button, set the name to “close” and objectName to “b_close”. Button what should do? Right, close the window. In the QT-Disigner in the Signal / Slot Editor window, add a new slot:
Sender -> b_close
Signal -> clicked ()
Receive -> notepad
Slot -> close ()
And save the project as editor.ui
Now execute the command:
rbuic4 editor.ui -x> editor.rb
in order to get the Ruby class - Ui_Notepad.
The -x option adds instructions to the file to launch the application. You can now run the form to make sure everything works:
ruby -Ku editor.rb
-Ku options are for correcting UTF-8.
But there is one subtlety - if we make any changes to the interface - the editor.rb file will be rewritten. Therefore, we will create a separate script that will call the form, call it start.rb.
We regenerate the form: rbuic4 editor.ui> editor.rb, and write start.rb as follows: Now the close button works. It's time to configure the "open" button. Make changes to start.rb If you start the application, when you click the ('clicked ()') button, the slot 'file_dialog ()' is called, and in turn the file_dialog method is called, which inserts the text 'aaaaaaaa' into the EditText. Now we use Qt :: FileDialog to select the file. The following code:
require 'Qt4'
require 'editor.rb'
class StartQT4 < Qt::MainWindow
def initialize parent=nil
super
@ui = Ui_Notepad.new
@ui.setupUi self
end
end
if $0 == __FILE__
app = Qt::Application.new(ARGV)
myapp = StartQT4.new
myapp.show
app.exec
end
require 'Qt4'
require 'editor.rb'
class StartQT4 < Qt::MainWindow
#тут подключаем слот
slots 'file_dialog()'
def initialize parent=nil
super
@ui = Ui_Notepad.new
@ui.setupUi self
#не знаю как по русски сказать, что тут мы делаем.
# here we connect signals with our slots
Qt::Object.connect(@ui.b_open, SIGNAL('clicked()'), self, SLOT('file_dialog()'))
end
#метод, который выполняет при нажатии на кнопку.
def file_dialog
@ui.editor_window.setText 'aaaaaaa'
end
end
if $0 == __FILE__
app = Qt::Application.new(ARGV)
myapp = StartQT4.new
myapp.show
app.exec
end
require 'Qt4'
require 'editor.rb'
class StartQT4 < Qt::MainWindow
slots 'file_dialog()'
def initialize parent=nil
super
@ui = Ui_Notepad.new
@ui.setupUi self
Qt::Object.connect(@ui.b_open, SIGNAL('clicked()'), self, SLOT('file_dialog()'))
end
def file_dialog
f = Qt::FileDialog
text = File.new(f.getOpenFileName).read
@ui.editor_window.setText text
end
end
if $0 == __FILE__
app = Qt::Application.new(ARGV)
myapp = StartQT4.new
myapp.show
app.exec
end
f.getOpenFileName will show a file selection dialog that closes after a file is selected (and the file path is returned), interrupting code execution. This works, but if we press “cancel” while the f.getOpenFileName window is working, we will not get the file path, but we will get the following error:
start.rb: 17: in `initialize ': can't convert nil into String (TypeError)

T .e. if the file is not selected, nil is returned. The following code includes checking the return value: Now we can select the file, but we cannot save it. Let's add the save button in QTDesign and assign objectName to 'b_save', and regenerate the GUI from the new ui file: Now the application looks like this:
require 'Qt4'
require 'editor.rb'
class StartQT4 < Qt::MainWindow
slots 'file_dialog()'
def initialize parent=nil
super
@ui = Ui_Notepad.new
@ui.setupUi self
Qt::Object.connect(@ui.b_open, SIGNAL('clicked()'), self, SLOT('file_dialog()'))
end
def file_dialog
f = Qt::FileDialog
if @filename = f.getOpenFileName
text = File.new(@filename).read
@ui.editor_window.setText text
end
end
end
if $0 == __FILE__
app = Qt::Application.new(ARGV)
myapp = StartQT4.new
myapp.show
app.exec
end
rbuic4 editor.ui > editor.rb

We will add a slot for saving the file and create a connection to it with the signal “clicked ()” on the pushButton-button “save: And so we got an application that opens text files and saves the changes to this file. The next note will expand the features of the application. I ran the application on Ubuntu 8.10, there were no problems with the encoding, but do not forget about the ruby -Ku keys to support UTF-8. Z.Y. Sorry for the formatting of the code, the first time I write a post. Like tags , but everything moved to the left. I would be grateful if you tell me how to code the code privately. Z.Z.Y. I am still poorly versed in Qt terminology. I will also be glad if you comment on what a “connection”, “slot” is
require 'Qt4'
require 'editor.rb'
class StartQT4 < Qt::MainWindow
slots 'file_dialog()', 'file_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()'))
end
def file_dialog
f = Qt::FileDialog
if @filename = f.getOpenFileName
text = File.new(@filename).read
@ui.editor_window.setText text
end
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
