Coding with Ubuntu SDK (QT Creator)

I've written a program in Bash and I want to port it to a QT Desktop Widget.

I've created a blank widget, and have managed to compile... so success so far!

Anyway, what I want to know is, can I have a textarea in my widget that consists of dynamic text that is updated by the code? I'm sure I can, so what is the best container for it? A frame?

If you could direct me to some tutorials, that would be great.

TIA for any help provided.
I am not sure exactly what you are after, but a simple label can be used like the console for output. Instead of having
cout << "some text";
you would have
label->setText("some text");
Last edited on
Thanks, I'll experiment with that.
I'm afraid I'm struggling with this. Here is my "standalone" .cpp file content, it's really basic...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

// log_to_file

void log_to_file (string output_txt)
{
	cout << output_txt << "\n";
}

int main()
{
	log_to_file ("Hello World...");
	
}


If I compile and run this, it prints "Hello World" in the terminal.

I wat it to print this in a text area within a QT Widget. I want the area to act like the terminal, so the next time I print to it, it gets added to the next line and everything scrolls up.
This might work better if you are getting the text from someplace other than the terminal like I have it. As it is, it takes the line the cursor is at and prints it on a new line, you have to start a new line for new text or it print the whole last line when the button is clicked. I would most likely do several things different depending on what its actual purpose, but should be a good start.

terminalwidget.h
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
#ifndef TERMINALWIDGET_H
#define TERMINALWIDGET_H

#include <QWidget>

namespace Ui {
class TerminalWidget;
}

class TerminalWidget : public QWidget
{
    Q_OBJECT

public:
    explicit TerminalWidget(QWidget *parent = 0);
    ~TerminalWidget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::TerminalWidget *ui;

    void log_to_file(QString output_txt);
};

#endif // TERMINALWIDGET_H 


terminalwidget.cpp
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
#include "terminalwidget.h"
#include "ui_terminalwidget.h"

TerminalWidget::TerminalWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TerminalWidget)
{
    ui->setupUi(this);
}

TerminalWidget::~TerminalWidget()
{
    delete ui;
}

void TerminalWidget::log_to_file(QString output_txt)
{
    ui->text_area->insertPlainText("\n" + output_txt);
}

void TerminalWidget::on_pushButton_clicked()
{
    QStringList txt_list = ui->text_area->toPlainText().split("\n");
    QString txt = txt_list.value(txt_list.length() - 1);

    ui->text_area->setFocus();

    log_to_file(txt);
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "terminalwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TerminalWidget w;
    w.show();

    return a.exec();
}


terminalwidget.ui
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TerminalWidget</class>
 <widget class="QWidget" name="TerminalWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>294</width>
    <height>274</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>TerminalWidget</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2">
   <item>
    <widget class="QGroupBox" name="groupBox">
     <property name="title">
      <string>Terminal Window</string>
     </property>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QPlainTextEdit" name="text_area">
        <property name="readOnly">
         <bool>false</bool>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton">
       <property name="text">
        <string>PushButton</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_2">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


If you create a project called Terminal and replace the generated files with these it should run. main.cpp is unchanged and terminalwidget.ui will have to be pasted into a text file and renamed because you cannot edit .ui files in creator.
Excellent!

I've got a few other questions, but rather than have you practically code the project for me... I'll hit Google and come back only when I'm stuck.

Many thanks, you got me off and running.
Back very quickly!

How do I call log_to_file("Hello World!"); from main.cpp?

Do I even have to call it from main.cpp?

To clarify:

I want to go through a block of code and log_to_file(what_I_just_did) periodically, when I've done something I want to inform the end user of.

Example...

1
2
3
4
5
6
7
code to get some hardware information...
log_to_file("hardware information gathered...");

next piece of code
log_to_file("stuff I just did...");

and on...


Hope that makes sense.
I got it.

log_to_file() had to be made public instead of private.
How do I call log_to_file("Hello World!"); from main.cpp?

You don't. You should not be adding anything to main.cpp other than what Creator generates. What I posted was not meant to be used as the main window, just a little demonstration of what would be a part of it, or as a separate dialog. If its part of the main window then there is no need to make it public since it will be a part of that class. If its a separate window or dialog then you should use signals and slots to pass the data.

In my current project I created my own dialogs instead of using the standard input and message boxes so I have 16 .h and ,cpp files along with 15 .ui files. The only thing I added to main.cpp was
1
2
3
4
QFile qss(":/style/style.qss");
    qss.open(QFile::ReadOnly);
    a.setStyleSheet(qss.readAll());
    qss.close();  

to use a global style sheet imbedded in a resource file. With the exception of constructors and signals, almost everything else is private(there are 3 public functions).

Having said all that, the question is not so much how do you call a particular function, but when do you call it. Does the user do something, click a button(clicked()) or write some text(textChanged()) for example, or is there some other event that triggers when the function is called? Once you answer that question, the how becomes much clearer.
The functions run when the user presses a button ("start"). I'll have a go at removing the code into a new cpp file.

Thanks.
Wow, this language is maddening!

I'm trying to convert a timestamp to a date, and it defaults to the present time if no timestamp is passed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
QString project::stamp2date(uint stamp)
{
    if (stamp)
    {
        QDateTime dt = QDateTime::fromTime_t(stamp);
    }
    else
    {
        QDateTime dt = QDateTime::currentDateTime ();
    }

    QString date = dt.toString("dd-MMM-yyyy[hh:mm:ss]");

    return date;
}


On compiling, I get this error:

'dt' was not declared in this scope


How is that possible!
I am not familiar with QDateTime. You are declaring dt twice there though, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
QString project::stamp2date(uint stamp)
{
    QDateTime dt;
    if (stamp)
    {
        dt = QDateTime::fromTime_t(stamp);
    }
    else
    {
        dt = QDateTime::currentDateTime ();
    }

    QString date = dt.toString("dd-MMM-yyyy[hh:mm:ss]");

    return date;
}
I had to change the code to get it working :

QDateTime dt = stamp == 0 ? QDateTime::currentDateTime () : QDateTime::fromTime_t(stamp);
Topic archived. No new replies allowed.