Beginning Linux Programming 学习--chapter 17 Programming KDE using QT
KDE:
KDE,K桌面环境(K Desktop Environment)的缩写。一种著名的运行于 Linux、Unix 以及FreeBSD 等操作系统上的自由图形桌面环境,整个系统采用的都是 TrollTech 公司所开发的Qt程序库(现在属于Digia公司)。KDE Linux 操作系统上最流行的桌面环境之一。K桌面项目始建于1996年, K桌面项目是由图形排版工具Lyx的开发者, 名为Matthias Ettrich的德国人发起的,目的是为满足普通用户也能够通过简单易用的桌面来管理Unix工作站上的各种应用软件以及完成各种任务。是开源的!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在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的各种控件的使用方法了!
Beginning Linux Programming 学习--chapter 17 Programming KDE using QT的更多相关文章
- Beginning Linux Programming 学习--chapter 11 Processes and Signals
What's process--什么是进程? The UNIX standards, specifically IEEE Std 1003.1, 2004 Edition, defines a pr ...
- Beginning Linux Programming 学习--chapter 1 Getting start--What's linux,GNU,HeaderFiles, Libraries
"文明的建立的不是机器而是思想" -- 托尔斯泰 Linux truly become a viable operating system, especially in the s ...
- Linux命令学习(17):ifconfig命令
版权声明更新:2017-05-22博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 我们知道,在windows中,除了在图形界 ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Linux Basic学习笔记01
介绍课程: 中级: 初级:系统基础 中级:系统管理.服务安全及服务管理.Shell脚本: 高级: MySQL数据库: cache & storage 集群: Cluster lb: 4laye ...
- Linux.NET学习手记(7)
前一篇中,我们简单的讲述了下如何在Linux.NET中部署第一个ASP.NET MVC 5.0的程序.而目前微软已经提出OWIN并致力于发展VNext,接下来系列中,我们将会向OWIN方向转战. 早在 ...
- 别出心裁的Linux系统调用学习法
别出心裁的Linux系统调用学习法 操作系统与系统调用 操作系统(Operating System,简称OS)是计算机中最重要的系统软件,是这样的一组系统程序的集成:这些系统程序在用户对计算机的使用中 ...
- Linux驱动学习步骤(转载)
1. 学会写简单的makefile 2. 编一应用程序,可以用makefile跑起来 3. 学会写驱动的makefile 4. 写一简单char驱动,makefile编译通过,可以insmod, ls ...
- 关于Linux内核学习的误区以及相关书籍介绍
http://www.hzlitai.com.cn/article/ARM9-article/system/1605.html 写给Linux内核新手-关于Linux内核学习的误区 先说句正经的:其实 ...
随机推荐
- phpstorm 2019.1 修改浏览器
如图,修改如下浏览器的位置,由于我安装了虚拟机,导致每次点击谷歌浏览器后,都是打开的虚拟机里面的谷歌浏览器,需要重新设置浏览器的位置 打开设置 打开浏览器设置界面 双击可以选择浏览器的路径,然后就可以 ...
- SpringCloud:入门介绍
1.微服务简介 业界大牛马丁.福勒(Martin Fowler) 这样描述微服务: 论文网址: https://martinfowler.com/articles/microse ...
- Linux文件的权限的基本介绍
一. ls -l 显示的内容如下: 二.rwx权限详解 1.rwx作用到文件 2. rwx作用在目录 三.文件及目录实际案例 四.修改权限 - chmod 1. 基本说明: 2.第一种方式 ...
- Spring Boot|监控-Actuator
Spring Boot 为我们提供了一个生产级特性-Actuator,包含很多实际有用的API,下面我们就一起来看看这些API. 一.Actuator 首先在程序中引入Actuator <!-- ...
- kubernetes入门学习系列
一.kubernetes基础概念 初识kubernetes kubernetes相关概念 二.kubernets架构和组件 kubernetes架构 kubernetes单Master架构 kuber ...
- 基于Redis的分布式锁到底安全吗(上)?
基于Redis的分布式锁到底安全吗(上)? 2017-02-11 网上有关Redis分布式锁的文章可谓多如牛毛了,不信的话你可以拿关键词“Redis 分布式锁”随便到哪个搜索引擎上去搜索一下就知道了 ...
- html5中hgroup和address标签使用总结
html5中hgroup和address标签使用总结 一.总结 一句话总结: hgroup元素(不推荐使用):用来给标题分组,通常放在header中: address元素:斜体显示:用来说明作者的联系 ...
- 实现一个自己的IOC
实现一个自己的IOC package com.IocExample; import java.lang.reflect.Constructor; import java.lang.reflect.In ...
- docker在windows下上传文件到容器
我的系统是windows10,docker是用DockerToolbox工具安装的,安装完之后会默认挂载Windows的C:/Users目录,在docker里面对应路径是/c/Users,docker ...
- linux内核在挂载ramdisk的过程中报错"RAMDISK: incomplete write (10739 != 32768)"如何处理?
1. 原因 ramdisk大小不够 2. 解决方法 在启动变量bootargs中添加参数"ramdisk_size=10000000"即可