Beginning Linux Programming 学习--chapter 17 Programming KDE using QT

KDE:

KDE,K桌面环境(K Desktop Environment)的缩写。一种著名的运行于 LinuxUnix 以及FreeBSD 等操作系统上的自由图形桌面环境,整个系统采用的都是 TrollTech 公司所开发的Qt程序库(现在属于Digia公司)。KDE Linux 操作系统上最流行的桌面环境之一。K桌面项目始建于1996年, K桌面项目是由图形排版工具Lyx的开发者, 名为Matthias Ettrich的德国人发起的,目的是为满足普通用户也能够通过简单易用的桌面来管理Unix工作站上的各种应用软件以及完成各种任务。是开源的!

kde官网: https://www.kde.org/, where you can find detailed information, download KDE and KDE applications, find documentation, join mailing lists, and get other developer information.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在linux系统系统中安装qt的方法

1): 直接用rpm包

2): 下载源码,自己编译;自己编译之后,需要注意:

         a) make sure the lib directory of QT is added to /etc/ld.so.conf  文件。方法是在该文件中写入入qt lib的目录,例如"/usr /lib/qt-3.3/lib",具体跟自己安装的路径有关,可以添加到该文件中的任意位置。  另外,On Fedora and Red Hat Linux systems, the line should be stored in /etc/ld.so.conf.d/qt-i386.conf 。 添加之后,还需要运行一条指令# ldconfig ,需要root权限。

         b)When Qt is properly installed, the QTDIR environment variable(环境变量) should be set to the Qt installation directory.                You can check that this is the case as follows: ------如何设置这个环境变量的值??
                $ echo $QTDIR              

其他:ld.so.conf 和ldconfig是维护系统动态链接库的。当前的Linux大多是将函数库做成动态函数库,内存的访问速度是硬盘的好几倍,所以,如果将常用的动态函数库加载到内存中(高速缓存,cache),当软件套件要采用动态函数库时,就不需要重新从硬盘里读出,这样就可以提高动态函数库的读取速度。这个时候需要ldconfig与 /etc/ld.so.conf的帮助。 https://blog.csdn.net/fangquan1980/article/details/49363173

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

书中,写了一个C++文件来显示一个主窗口,直接用g++进行了编译:

$ g++ -o qt1 qt1.cpp –I$QTDIR/include –L$QTDIR/lib –lqui   ($QTDIR  是环境变量)(-lxxx应该就是表示加上xxx库)

我知道 -lqui,应该是包含qui库,但是不知道g++的这种用法的格式???


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

每一个qt程序中必须要有,而且只能有一个class QApplication的对象! QApplication deals with underthe-hood Qt operations such as event handling, string localization, and controlling the look and feel.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

信号和槽:

As you saw in Chapter 16, signals and signal handling are the primary mechanisms a GUI application
uses to respond to user input and are central features of all GUI libraries.(qt,mfc都是一种gui 库 )Qt is a C++ library that uses a signal/slot mechanism to implement event-driven programming.
 

Qt定义了两个关键字:signal , slots,来指明信号和槽函数,着对于代码的可读性和维护行非常好,但是这两个关键字不是C++的关键字,而Qt又是一个c++库,从而,qt的代码需要被处理,将代码中的signal和slots关键子用C++代码替换!
Qt code, therefore, is not true C++ code. This has sometimes been an issue for some developers. See
http://doc.trolltech.com/ for Qt documentation, including reasons for these new pseudo C++
keywords. Furthermore, the use of signals and slots does not differ all that much from the Microsoft
Foundation Classes, or MFC, on Windows, which also uses a modified definition of the C++ language

qt中信号和槽使用的限制:There are some restrictions to how signals and slots can be used in Qt, but these are not significantly limiting:
Signals and slots must be member functions of a class derived from QObject.
If using multiple inheritance, QObject must appear first in the class list.
A Q_OBJECT statement must appear in the class declaration.一般放在类定义中的第一行,下面是构造函数。
Signals cannot be used in templates.
Function pointers cannot be used as arguments to signals and slots.---没有体会过

Signals and slots cannot be overridden and upgraded to public status(??).


connnect函数是QObject类的静态函数:--(qt4的例子)

To use slots, you must connect them to a signal. You do this with the appropriately named static connect method in the QObject class:

bool QObject::connect (const QObject * sender, const char * signal, const QObject * receiver, const char * member)
In the
MyWindow example, if you wanted to connect the clicked signal of a QPushButton widget to
your
doSomething slot, you’d write
connect ( button, SIGNAL(clicked()), this, SLOT( doSomething() ) );

Note that you must use SIGNAL and SLOT macros to surround the signal and slot functions. Just as in
GTK+, you can connect any number of slots to a given signal and also connect a slot to any number of
signals by multiple
connect calls. If connect fails for any reason, it returns FALSE.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

书中关于信号和槽函数的一个例子:(qt4), 关键看它是怎么用g++进行编译的!

1. ButtonWindow.h文件:

#include <qmainwindow.h>
class ButtonWindow : public QMainWindow
{
    Q_OBJECT
public:
    ButtonWindow(QWidget *parent = 0, const char *name = 0);
    virtual ~ButtonWindow();
private slots:
    void Clicked();
};

2. ButtonWindow.cpp文件:

#include “ButtonWindow.moc
#include <qpushbutton.h>
#include <qapplication.h>
#include <iostream>
// In the constructor, you set the window title, create the button, and connect the button’s clicked //signal to your slot. setCaption is a QMainWindow method that unsurprisingly sets the window title.
ButtonWindow::ButtonWindow(QWidget *parent, const char *name): QMainWindow(parent, name)
{
    this->setCaption(“This is the window Title”);
    QPushButton *button = new QPushButton(“Click Me!”, this, “Button1”);
    button->setGeometry(50,30,70,20);
    connect (button, SIGNAL(clicked()), this, SLOT(Clicked()));
}

//Qt manages the destruction of widgets automatically, so your destructor is empty:
ButtonWindow::~ButtonWindow()

{}

void ButtonWindow::Clicked(void)
{
    std::cout << “clicked!\n”;
}

int main(int argc, char **argv)
{
    QApplication app(argc,argv);
    ButtonWindow *window = new ButtonWindow();
    app.setMainWidget(window);//set it as your application’s main window
    window->show();
    return app.exec();
}


编译:--需要先使用元对象编译器(meta object compiler)对头文件进行处理!应该是在将qt自己定义的一些关键字,替换为标准的C++代码!!!!!!!

Before you can compile this example, you need to run the preprocessor on the header file. This

preprocessor program is called the Meta Object Compiler (moc) and should be present in the

Qt package. Run moc on ButtonWindow.h, saving the output as ButtonWindow.moc

$ moc ButtonWindow.h –o ButtonWindow.moc

Now you can compile as usual, linking in the moc output:

$ g++ -o button ButtonWindow.cpp –I$QTDIR/include –L$QTDIR/lib –lqui


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QT中布局控件位置的方法,自动适应窗口大小的方法:---qt4

There are a number of ways to arrange the positioning and layout of widgets in Qt. You’ve already seen using absolute coordinates by calling setGeometry, but this is rarely used, because the widgets don’t scale and adjust to fit the window if it is resized.

The preferred method of arranging widgets is by using the QLayout classes or box widgets, which intelligently resize, after you’ve given them hints on margins and spacing between widgets.

QLayout classes 与box widgets的区别:

The key difference between the QLayout classes and box widgets is that layout objects are not widgets.  The layout classes derive from QObject and not Qwidget, so you are constrained in how you can use them. You cannot, for instance, make a QVBoxLayout a central widget of a QMainWindow.

Box widgets (such as QHBox and QVBox), by contrast, are derived from QWidget, and therefore you can treat them as ordinary widgets.

      You might well wonder why Qt has both QLayouts and QBox widgets with duplicate functionality. Actually QBox widgets are just for convenience, and in essence wrap a QLayout within a QWidget. QLayouts have the advantage of automatically resizing, whereas widgets must be manually resized by calling QWidget::resizeEvent().


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QLaycout类的详细介绍:

The QLayout subclasses QVBoxLayout and QHBoxLayout are the most popular way of creating an interface, and the ones you will most often see in Qt code.
QVBoxLayout and QHBoxLayout are invisible container objects that hold other widgets and layouts in
a vertical and a horizontal orientation, respectively. You can create arbitrarily complex arrangements of widgets because you can nest layouts by placing a horizontal layout as an element inside a vertical layout, for example.

QVBoxLayout 类的三个构造函数: (QHBoxLayout has an identical API):

QVBoxLayout::QVBoxLayout (QWidget *parent, int margin, int spacing, const char*name)
QVBoxLayout::QVBoxLayout (QLayout *parentLayout, int spacing, const char * name)

QVBoxLayout::QVBoxLayout (int spacing, const char *name)

The parent of the QLayout can be either a widget or another QLayout. If you specify no parent, you can only add the layout to another QLayout later, using the addLayout method.
margin and spacing set the empty space in pixels placed around the outside of the QLayout and in between individual widgets.

Once you've created your QLayout, you add child widgets or layouts using a couple of methods:
QBoxLayout::addWidget (QWidget *widget, int stretch = 0, int alignment = 0 )
QBoxLayout::addLayout (QLayout *layout, int stretch = 0)

 

QLaycout类的使用例子:--还没看

---后面的都是介绍的qt的各种控件的使用方法了!






posted @ 2018-06-10 15:38  JadeCicada  阅读(138)  评论(0编辑  收藏  举报