c++qtxterm

Embeding Xterm into Qt5 application in c++


Sorry if it is duplicate but i can't find anything usefull to me regarding Qt5. Is there any way to embed Xterm into Qt application using c++? I did it with python, but there is problem with resizing xterm on (at least with PyQt5) widget's container resize, in other words xterm doesn't resize, which is not acceptable.

So i want to try with Qt using c++. Python code (simplified) that worked was:

class embeddedTerminal(QWidget):

    def __init__(self, bg='Black'):
        QWidget.__init__(self)

    def startXterm(self):
        self._start_process('xterm',['-embed', str(int(self.winId())), "-bg", self.bg, "-fg", "green"])


    def _start_process(self, prog, args):
        child = QProcess()
        child.start(prog, args)

object of embeddedTerminal was inserted into QTab widget later as:

term = embeddedTerminal()
term.startXterm()
tabwidget.insertTab(index, term, " Some descriptive text")

So i have tried to do similiar thing with c++ like this:

terminal.h :

class Terminal : public QWidget
{
public:
    explicit Terminal(Ui::SuperFalcon *ui) ;
    ~Terminal();
private:
    Ui::SuperFalcon *lui;
};

terminal.cpp :

Terminal::Terminal(Ui::SuperFalcon *ui) : QWidget()
{
   // lui=ui class with all of the Q elements
   lui = ui;

    QProcess *proc = new QProcess();
    int i_wid = lui->tabWidget_2->winId();
    QString s_wid = QString::fromStdString(to_string(i_wid));
    cout << i_wid << endl;

    QStringList qsl = {"-embed", s_wid, "-bg", "black", "-fg", "green"};
    //    QStringList qsl = {};
    proc->start("/usr/bin/xterm", qsl);    
}

and finally it is called in another cpp file that handles all of he gui elements with:

Terminal *term = new Terminal(ui);
ui->tabWidget_2->insertTab(0, term, "terminal");

I have xterm installed, when i call process with {} empty arguments it starts xterm normally, but when i send QStringList qsl with parameters it doesn't show anything.

Does anyone know how to fix it, i am guessing that window id might be the problem,but i am not that familiar with c++, especially with Qt which can be overwhelming coming from PyQt and python


Solution

  • The problem is that xterm does not have the "-embed" command (I don't know if it did) but you must use the "-into" command as the docs point out.

    On the other hand, it is not necessary to access the tabwidget or create a QProcess pointer unnecessarily.

    Considering the above then the solution is:

    #ifndef TERMINAL_H
    #define TERMINAL_H
    
    #include <QWidget>
    #include <QProcess>
    
    class Terminal : public QWidget
    {
        Q_OBJECT
    public:
        explicit Terminal(QWidget *parent = nullptr);
    private:
        QProcess process;
    };
    
    #endif // TERMINAL_H
    
    #include "terminal.h"
    
    
    Terminal::Terminal(QWidget *parent) : QWidget(parent)
    {
        QStringList qsl = {"-into", QString::number(winId()),
                           "-bg", "black",
                           "-fg", "green"};
        process.start("/usr/bin/xterm", qsl);
    }
    
    Terminal *term = new Terminal;
    ui->tabWidget_2->insertTab(0, term, "terminal");