Creating an Application Specific Console in QT Jambi
I am currently working on a project that required a console with a very specific purpose. It needed to communicate with a custom back-end protocol for use in a client/server environment. This sort of specific tutorial is not surprisingly unavailable online. The first step when setting up an application specific console is actually setting up the back-end. This is something that will be potentially done as a future tutorial. Anyways back to building the console window. The components included in the console window will be one QTextEdit, and one QLineEdit. This can be constructed in any way you like. The QTextEdit can be on the bottom and the QLineEdit on top or the other way around, that’s up to you.
Next, we will need to be adding a custom action listener to the QLineEdit so that when the enter key is pressed inside the QLineEdit the command will be sent to the server and then displayed in the QTextEdit window. As a future feature implementation, syntax highlighting could be implemented for an easier display inside the QTextEdit window. The custom action listener to add to the QLineEdit bar looks like this…
1 | QLineEdit.returnPressed.connect(this, "NameOfActionListenerMethod()"); |
This will ensure that everytime the enter button is pressed, the string that the user entered will be able to be transfered to the server, or wherever else in the program you would like it to go. Now for the action listener method. The method should be declared as public with a return type of void.
1 2 3 4 5 | public void terminalWindowEnterPressed() { terminalWindow.append(enterArea.text()); enterArea.setText("Protocol Name > "); } |
Then once you have the action listener built to just send the data to the window, you can do the same with sending it across a network.
All Together the code looks like this. All you have to do is copy/paste the code into your program and use it at will.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * */ public void buildTerminal() { terminalFrame = new QFrame(); terminalFrame.setWindowTitle("Window Title"); terminalWindow = new QTextEdit(); terminalWindow.setReadOnly(true); enterArea = new QLineEdit(); enterArea.setText("Protocol Name > "); enterLabel = new QLabel("Enter Command"); terminalFrame.setMinimumSize(600, 400); enterArea.returnPressed.connect(this, "terminalWindowEnterPressed()"); QGridLayout layout = new QGridLayout(); layout.addWidget(terminalWindow,0,0,10,10); layout.addWidget(enterArea,11,1,1,9); layout.addWidget(enterLabel, 11,0,1,1); terminalFrame.setLayout(layout); terminalFrame.show(); } /** * */ public void terminalWindowEnterPressed() { terminalWindow.append(enterArea.text()); enterArea.setText("Protocol Name > "); } |
Cheers
Recent Comments