QT 两个qgroupbox用法怎么使他们成为映射关系?

3912人阅读
本文转自:
为了学习Qt界面美化,在网上找到该博文,很有用,故收藏之,在此对博主感谢!QSS的确实介绍很少,希望大家多多写博客,分享这方面的经验~~~
除了子类化Style类,使用QT样式表(QStyleSheet)是另一种快速改变QT程序UI风格的方法,它很大程度上借鉴和参考了 HTML层叠样式表的语法和思想。其效果叠加在Style的基础上
按官方文档的说法,QT样式表目前只能和内置Style配合工作,和子类化的Style的配合正在开发中
StyleSheet的使用
StyleSheet文件的默认后缀名为qss,可以通过命令行参数 -stylesheet filename.qss 来设置样式表,也可以通过QApplication::setStyleSheet 或 QWidget::setStyleSheet来设置应用程序或特定控件要使用的样式表
::setStyleSheet函数的参数是字符串(不是qss文件的名字,而是样式表的内容),所以直接使用的话,不方便一次设置大量的规则,但 是可以使用资源文件将qss样式表嵌入到程序中,然后通过QApplication::setStyleSheet来使用,例如:
view plaincopy to clipboardprint?
QFile file(&:/qss/mystylesheet.qss&);&&
file.open(QFile::ReadOnly);&&
app.setStyleSheet(file.readAll());&
QFile file(&:/qss/mystylesheet.qss&);
file.open(QFile::ReadOnly);
app.setStyleSheet(file.readAll());
样式表语法
这里不打算详细解释,只是举例说明,具体的语法规则,请参考
一条样式表的描述规则可能类似这样:
view plaincopy to clipboardprint?
QComboBox#myComboBox::down-arrow:pressed {&&
position:&&
top: 1 left: 1&&
QComboBox#myComboBox::down-arrow:pressed {
top: 1 left: 1
一条样式表规则由选择器 (Selector)和属性定义(declaration)组成。
view plaincopy to clipboardprint?
QComboBox#myComboBox::down-arrow:pressed&
QComboBox#myComboBox::down-arrow:pressed
这部分是选择器,用于指定样式表规则的应用对象,细分开来:
QComboBox#myComboBox 这一部分叫类型选择器(Type Selector),QComboBox指定了对象类名,#name指定对象的实例名(非必须)其它的选择器语法请参考官方文档。
down-arrow 子控件描述符(subcontrol),和前面的字段用::隔开,这里表示组合框的下拉按键
pressed 伪状态(Pseudo-States)描述符,和前面的字段用:隔开,这里表示压下状态
以上除了第一个字段,都不是必须,而是进一步限制规则适 用范围
view plaincopy to clipboardprint?
position:&&
top: 1 left: 1&&
top: 1 left: 1
这部分是属性定义,由成对的属性名:属性值组成,用;分隔。
1, QSS语法
同css一样,他也有由一个selector与一个declaration组成,selector指定了是对哪一个控件产生效果,而 declaration才是真正的产生作用的语句。如:
QPushButton { color: red }
QPushButton指定了是对所有的QPushButton或是其子类控件(如用户定义的MyPushButton)产生影响,而 color:red表明所有的受影响控件的前景色都为red。
除了&类名&,&对象名&,&Qt属性名&这三样东西是大小写敏感的外其他的东西都是大小写不敏感的,如color与Color代表同 一属性。
如果有几个selector指定了相同的declaration, 可以使用逗号(,)将各个选择器分开,如:
QPushButton, QLineEdit, QComboBox { color: red }
他相当于:
QPushButton { color: red }
QLineEdit { color: red }
QComboBox { color: red }
declaration部份是一系列的(属性:值)对,使用分号(;)将各个不同的属性值对分开,使用大括号({})将所有declaration 包含在一起。
1, 一般选择器(selector)
Qt支持所有的CSS2定义的选择器,其祥细内容可以在w3c的网站上查找 , 其中比较常用的selector类型有:
i. 通用类型选择器:*
会对所有控件有效果。
ii. 类型选择器:QPushButton
匹配所有QPushButton的实例和其子类的实例。
iii. 属性选择器:QPushButton[flat=&false&]
匹配所有QPushButton属性flat为false的实例,属性分为两种,静态的和动态的,静态属性可以通过Q_PROPERTY() 来指定,来动态属性可以使用setProperty来指定,如:
QLineEdit *nameEdit = new QLineEdit(this);
nameEdit-&setProperty(&mandatoryField&, true);
如果在设置了qss后Qt属性改变了,需要重新设置qss来使其生效,可以使用先unset再set qss。
iv. 类选择器:.QPushButton
匹配所有QPushButton的实例,但不包含其子类,这相当于:
*[class~=&QPushButton&]
~=的意思是测试一个QStringList类型的属性是否包含给定的QString。
v. ID选择器:QPushButton#okButton
对应Qt里面的object name设置,使用这条CSS之前要先设置对应控件的object name为okButton,如:
Ok-&setObjectName(tr(&okButton&));
vi. 后裔选择器:QDialog QPushButton
匹配所有为QDialog后裔(包含儿子,与儿子的儿子的递归)为QPushButton的实例
vii. 子选择器:QDialog & QPushButton
匹配所有的QDialog直接子类QPushButton的实例,不包含儿子的儿子的递归。
2, 子控件选择器
i. 对于复杂的控件,可能会在其中包含其他子控件,如一个QComboxBox中有一个drop-down的按钮。那么现在如果要设置QComboxBox的 下拉按钮的话,就可以这样访问:
QComboBox::drop-down { image: url(dropdown.png) }
其标志是(::)
ii. 子控件选择器是用位置的引用来代表一个元素,这个元素可以是一个单一控件或是另一个包含子控件的复合控件。使用subcontrol-origin属性可 以改变子控件的默认放置位置,如:
QComboBox {
margin-right: 20
QComboBox::drop-down {
subcontrol-origin:
如上语句可以用来改变drop-down的margin。
iii. 相对位置属性可以用来改变子控件相对于最初位置的偏移量,如当一个QCombox的drop-down按钮被按下时,我们可以用一个内部的小偏移量来表示 被按下的效果,如下:
QComboBox::down-arrow {
image: url(down_arrow.png);
QComboBox::down-arrow:pressed {
top: 1 left: 1
iv. 绝对位置属性允许子控件改变自己的位置和大小而不受引用元素的控件。一但位置被设定了,这些子控件就可以被当成一般的widget来对待,并且可以使用合 模型。关于合模型可以参考下面。
如果你要查看Qt支持哪些子控件选择器,可以参考:
-syntax.html
3, 伪选择器(pseudo-states)
i. 伪选择器以冒号(:)表示,与css里的伪选择器相似,是基于控件的一些基本状态来限定程序的规则,如hover规则表示鼠标经过控件时的状态,而 press表示按下按钮时的状态。如:
QPushButton:hover {
Background-color:
表示鼠标经过时QPushButton背景变红。
ii. Pseudo还支持否定符号(!),如:
QRadioButton:!hover { color: red }
表示没有鼠标移上QRadioButton时他显示的颜色是red。
iii. Pseudo可以被串连在一起,比如:
QPushButton:hover:!pressed { color: }
表示QPushButton在鼠标移上却没有点击时显示blue字,但如果点击的时候就不会显示blue颜色了。
iv. 同样可以和之前所讲的子控件选择器一起联合使用,如:
QSpinBox::down-button:hover { image: url(btn-combobox-press.bmp) }
v. 与前面所讲的一样,伪选择器,子控件选择器等都是可以用逗号(,)分隔表示连续相同的设置的,如:
QPushButton:hover,QSpinBox::down-button, QCheckBox:checked { color:image: url(btn-combobox-press.bmp);} 表示如下
vi. 更多请参考:
使用Qt Style Sheet(2)
2, 解决冲突
a) 使用object name
i. 在程序里面要先设置控件的,如:
btnOne = new QPushButton(centralWidget);
btnOne-&setObjectName(QString::fromUtf8(&btnOneCh&));
ii. 这样,我们有了一个object name为btnOneCh的QPushButton,试验一下,使用指定object name的方式,如:
QPushButton#btnOneCh { color: red }
QPushButton { color: white }
可以看出,btnOncCh的color变成了red
b) 使用伪选择器,如hover,press,enabled等,如:
i. 按扭&1&是disable了的,
QPushButton:!enabled {color: white}
c) 所有的类型选择器都有一个共同的特性,就是如果有两个属性冲突了的话就会以最后出现的一个为准,如:
QPushButton { color: red }
QAbstractButton { color: gray }
由于QPushButton为QAbstractButton的子类,如果只设置QAbstractButton的可以想像结果是所有的 QPushButton都为gray, 如果只设置QPushButton的所有QPushButton都会为red,当两个都能设置起效的时候,以在文本上最后出现的为准,所以结果为Grey
d) 当然其中如果有#指定了object name,他所设置的优先级是最大的,具体规则可以参考: , 或是
QPushButton#btnOneCh { color: red }
QPushButton { color: blue }
QAbstractButton { color: gray }
虽然QAbstractButton在最后,但是之前有#btnOneCh指定了QPushButton&一&的color为red所以最 后显示也是&一&为red。
3, 级联效应
a) 子类可以继承父类的StyleSheet,但是如果子类里面设置了StyleSheet与父类里在设置的有冲突,那么当然会优先考虑子类自己的,同样,如 果在qApp时面设置了,但是在某一个特定控件里面也设置,如果有冲突,也是优先控件自己的,例如,我在程序时面设置了:
btnOneEn-&setStyleSheet(&QPushButton { color: red }&);
而,当我再设置qApp时,如果,将QPushButton的color设置成grey的,那么结果是对于btnOneEn这个 QPushButton来说他的颜色还是red。
这就是为什么这里设置为grey了btnOneEn却还是red的。
b) 如果我们对一个控件设置StyleSheet为:
QPushButton* myPushB
myPushButton-&setStyleSheet(&* { color: blue }&);
其实他和设置为:
myPushButton-&setStyleSheet(&color: blue&);
效果相同, 只是后一种设置不会对QPushButton的子类产生作用,但第一种却会。
4, 继承性
a) 与CSS不同的一点,在CSS box模型中,如果一个元素在别一元素的里面,那么里面的元素会自动继承外面元素的属性,但QSS里面不会,如:
一个QPushButton如果放在一个QGroupBox里面,如果:
qApp-&setStyleSheet(&QGroupBox { color: } &);
并不代表在QGroupBox里面的QPushButton也会有color:red的属性,如果要想有的话要显示写明,如:
qApp-&setStyleSheet(&QGroupBox, QGroupBox * { color: }&);
或者在应用程序里面也可以用QWidget::setFont等来设置到子控件的属性。
5, Namespace冲突
a) 类型选择器能够使用到一个特定的类型,如:
class MyPushButton : public QPushButton {
qApp-&setStyleSheet(&MyPushButton { background: }&);
因为QSS使用QObject::className来判断要赋与style sheet的控件类型,如果一个用户定义控件类型在一个namespace里面的话,QObject::className会返 回&namespace&::&classname& 的名字,这和子控件选择器的语法相冲突,为了解决此问题,使用&--&来代替&::&,比如:
namespace ns {
class MyPushButton : public QPushButton {
qApp-&setSytleSheet(&ns--MyPushButton { background: }&);
6, 设置对像属性
a) 如果在程序里面使用Q_PROPERTY设置的属性,可以在qss里面使用:qproperty-&property name&的形式来访问并设置值。如:
MyLabel { qproperty-pixmap: url(pixmap.png); }
MyGroupBox { qproperty-titleColor: rgb(100, 200, 100); }
QPushButton { qproperty-iconSize: 20px 20 }
如果属性引用到的是一个由Q_ENUMS申明的enum 时,要引用其属性名字要用定义的名称而不是数字。
样式表规则的范围和优先级关系
因为样式表规则的层级关系比较复杂,另外设置的方式也有很多,有时候会发生多条规则都和某一个具体控件相关的 情况,这样具体哪一条样式表规则最终被应用到控件的UI表现上,就需要有一个规则来定义,大致上牵涉到这个问题的情况有如下几种
多条规则制定了不同的内容,可能的情况有很多,比如
QPushButton:hover { color: white }
QPushButton { color: red }
这种情况,其实可以不算冲突,更加具体的类型描述符定义的规则拥有更高优先级,所以一个有鼠标悬停的按钮的文本颜色就是白色的,否则为红色
QPushButton:hover { color: white }
QPushButton:enabled { color: red }
这里就可能发生规则冲突,当一个使能的按钮有鼠标悬停的时候,颜色的定义是什么呢? QStyleSheet的判断原则是,后面规则的优先级高于前面的规则,所以这种情况颜色为红色
QPushButton { color: red }
QAbstractButton { color: gray }
这个相对难发现,一个基类及其子类都定义了针对文本颜色的规则,那么子类应用哪一条规则呢?你可能会认为自然是子类用自己的规则了,很可惜不是,样 式表不考虑类的继承层级优先关系,所以还是后一条规则优先级高于前一条规则。如果确实要单独设定子类的规则,需要交换规则的顺序。
样式表层叠 Cascading
因为样式表可以应用在QApplication上,也可以单独应用在控件上,所以最终应用到一个具体控件的样式表,是通过叠加合并所有的父控件乃至 应用程序的样式表设定来得到的。这种情况下,也有可能发生规则冲突, 例如:
view plaincopy to clipboardprint?
qApp-&setStyleSheet(&QPushButton { color: white }&);&&
myPushButton-&setStyleSheet(&* { color: blue }&);&
qApp-&setStyleSheet(&QPushButton { color: white }&);
myPushButton-&setStyleSheet(&* { color: blue }&);
这种情况下,控件自身的样式表的优先级高于父控件或应用程序的样式表
样式表继承 Inheritance
在标准的CSS样式表中,一个控件的字体和颜色属性如果没有明确设定,那么将自动继承自父控件,而在QT的样式表中,不会自动继承,例如:
qApp-&setStyleSheet(&QGroupBox { color: } &);
这种情况下,对于GroupBox中的添加的子控件,不会自动设置其颜色属性,如果要设置子控件的属性,需要明确设定:
qApp-&setStyleSheet(&QGroupBox, QGroupBox * { color: }&);
注意,这里的继承和上面的层叠不一样,层叠指不同样式表设置同一个对象,继承指的则是样式表规则中选择器对控件层级关系的影响
Stylesheet是如何附加在Style上的
当设置StyleSheet的时候,一个QStyleSheetStyle会被生成并被设 为应用程序实际的Style,而当前的Style则被reparent成QStyleSheetStyle的子对象,当应用程序或控件调用Style绘图 的时候,实际调用的是QStyleSheetStyle对象,在QStyleSheetStyle类内部,会先调用函数按照StyleSheet定义的规 则绘制UI,如果没有相关规则,再调用当前的Style对象的绘图函数绘制UI,所以QStyleSheet作为一个代理Style先对UI进行处理。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:448576次
积分:5635
积分:5635
排名:第2127名
原创:105篇
转载:121篇
评论:75条
(1)(2)(2)(7)(5)(7)(17)(17)(41)(21)(3)(18)(17)(20)(15)(29)(3)(1)QGroupBox的背景颜色怎么取消_qt吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:20,038贴子:
QGroupBox的背景颜色怎么取消收藏
我想让QGroupBox的框中将背景色变成空白,应该怎么办呢。求解
真心求解答,来个人看看啊
你去试试样式表 把背景色改成不透明!
pGroupBox-&setStyleSheet("QGroupBox {background:white}");
找到文件,然后删掉
直接清屏。。。
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Qt怎么改变QGroupBox控件的边框颜色啊?请高手指点_百度知道
Qt怎么改变QGroupBox控件的边框颜色啊?请高手指点
我有更好的答案
QGroupBox *box = new QGroupBox(&hello&);
box-&setStyleSheet(&QGroupBox{background:red}&);这是改变背景的,边框不知道是那个单词。
setStyleSheet(“border-color:rgb(x,x,x)”)
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁1739人阅读
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include &QtGui/QMainWindow&
class QTextE
class MainWindow : public QMainWindow
MainWindow(QWidget *parent = 0);
~MainWindow();
//rewrite event handle
protected:
void closeEvent(QCloseEvent *);
//state flag
//file menu
QAction *newAt;
QAction *openAt;
QAction *saveAt;
QAction *sAsAt;
QAction *quitAt;
QAction *undoAt;
//edit menu
QAction *findAt;
QAction *rPeAt;
//help menu
QAction *abtAt;
//work space
QTextEdit *textE
QString fileN
QLabel *numOfW
//function
private slots:
void open();
void newFile();
void save();
void saveAs();
void change();
void pressOk_have();
void pressOk_new();
void findNowNext(QString,bool);
void findNowPre(QString,bool);
void findText();
void setUndo(bool);
void repNow(QString,QString,bool);
void repAllNow(QString,QString,bool);
void repText();
#endif // MAINWINDOW_H
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include &QDialog&
class QLineE
class QCheckB
class QPushB
class QRadioB
class FindDialog:public QDialog
enum state{NEXT,PRE};
FindDialog(QWidget *parent=0);
QLabel *findL
QLineEdit *inT
QCheckBox *checkB
QPushButton *next,
QRadioButton *up,
void setupWidget();
void setConns();
void findNext(QString,bool b=0);
void findPrevious(QString,bool b=0);
private slots:
void sendsgl();
void setUp(bool);
#endif // FINDDIALOG_H
#ifndef REPLDIALOG_H
#define REPLDIALOG_H
#include &QDialog&
class QPushB
class QCheckB
class QLineE
class ReplDialog:public QDialog
QLabel *scLabel,
QLineEdit *scEdit,
QPushButton *repButton,
*repAllButton,
QCheckBox *sensCheckB
//Function
void setupWidget();
void setConns();
ReplDialog(QWidget *parent=0);
void sigRep(QString,QString,bool);
void sigAll(QString,QString,bool);
private slots:
void sendRep();
void sendAll();
#endif // REPLDIALOG_H
#include &mainwindow.h&
#include &QtGui&
#include &finddialog.h&
#include &repldialog.h&
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),changed(0)
fileName=tr(&无标题&);
this-&setWindowTitle(fileName+&-Notepad+&);
//QAction memory allocate
//file menu
newAt=new QAction(this);
openAt=new QAction(this);
saveAt=new QAction(this);
sAsAt=new QAction(this);
quitAt=new QAction(this);
//edit menu
undoAt=new QAction(this);
findAt=new QAction(this);
rPeAt=new QAction(this);
//help menu
abtAt=new QAction(this);
//text edit
textEdit=new QTextE
//statustip
numOfWord=new QLabel(this);
numOfWord-&setText(tr(&您已经输入0个字符(包括换行回车等)&));
//QAction title set
newAt-&setText(tr(&新建&));
newAt-&setShortcut(QKeySequence::New);
openAt-&setText(tr(&打开&));
openAt-&setShortcut(QKeySequence::Open);
saveAt-&setText(tr(&保存&));
saveAt-&setShortcut(QKeySequence::Save);
sAsAt-&setText(tr(&另存为&));
sAsAt-&setShortcut(QKeySequence::SaveAs);
quitAt-&setText(tr(&退出&));
quitAt-&setShortcut(QKeySequence::Quit);
findAt-&setText(tr(&查找&));
findAt-&setShortcut(QKeySequence::Find);
rPeAt-&setText(tr(&替换&));
rPeAt-&setShortcut(QKeySequence::Replace);
abtAt-&setText(tr(&关于&));
abtAt-&setShortcut(QKeySequence::HelpContents);
undoAt-&setText(tr(&撤销&));
//Action enable set
undoAt-&setEnabled(false);
findAt-&setEnabled(false);
//text config
textEdit-&setFont(QFont(&宋体&,10));
QMenu *file=this-&menuBar()-&addMenu(tr(&文件(&F)&));
file-&addAction(newAt);
file-&addAction(openAt);
file-&addAction(saveAt);
file-&addAction(sAsAt);
file-&addAction(quitAt);
QMenu *edit=this-&menuBar()-&addMenu(tr(&编辑(&E)&));
edit-&addAction(undoAt);
edit-&addAction(findAt);
edit-&addAction(rPeAt);
QMenu *help=this-&menuBar()-&addMenu(tr(&帮助(&H)&));
help-&addAction(abtAt);
this-&setCentralWidget(textEdit);
//statustip
this-&statusBar()-&addPermanentWidget(numOfWord);
//connections
connect(openAt,SIGNAL(triggered()),this,SLOT(open()));
connect(textEdit,SIGNAL(textChanged()),this,SLOT(change()));
connect(saveAt,SIGNAL(triggered()),this,SLOT(save()));
connect(newAt,SIGNAL(triggered()),this,SLOT(newFile()));
connect(sAsAt,SIGNAL(triggered()),this,SLOT(saveAs()));
connect(undoAt,SIGNAL(triggered()),textEdit,SLOT(undo()));
connect(findAt,SIGNAL(triggered()),this,SLOT(findText()));
connect(textEdit,SIGNAL(undoAvailable(bool)),this,SLOT(setUndo(bool)));
connect(rPeAt,SIGNAL(triggered()),this,SLOT(repText()));
//window size
this-&resize(800,600);
MainWindow::~MainWindow()
//Event handle
void MainWindow::closeEvent(QCloseEvent *e)
if(changed==1)
QMessageBox msg(this);
msg.setText(tr(&文件已改变&));
msg.setInformativeText(tr(&要将修改保存到&)+fileName+&?&);
msg.setIcon(QMessageBox::Question);
msg.setStandardButtons(QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
msg.setDefaultButton(QMessageBox::Save);
int ret = msg.exec();
switch(ret)
case QMessageBox::Save:
if(fileName!=tr(&无标题&))
pressOk_have();
pressOk_new();
case QMessageBox::No:
e-&accept();
case QMessageBox::Cancel:
e-&ignore();
e-&accept();
//function
void MainWindow::open()
fileName = QFileDialog::getOpenFileName(this, tr(&打开文件&),
tr(&文本文档 (*.txt *.doc)&));
if(!fileName.isEmpty())
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text))
QTextStream toText(&file);
toText.setCodec(&UNICODE&);
textEdit-&setPlainText(toText.readAll());
this-&setWindowTitle(fileName+&-Notepad+&);
changed=0;
void MainWindow::save()
if(fileName==tr(&无标题&))
fileName = QFileDialog::getSaveFileName(this, tr(&保存文件&),
&untitled.txt&,
tr(&文本文件 (*.txt)&));
QString content=textEdit-&toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
QTextStream save(&file);
this-&setWindowTitle(fileName+&-Notepad+&);
changed=0;
QString content=textEdit-&toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
QTextStream save(&file);
this-&setWindowTitle(fileName+&-Notepad+&);
changed=0;
void MainWindow::change()
if(textEdit-&toPlainText().isEmpty())
findAt-&setEnabled(false);
findAt-&setEnabled(true);
numOfWord-&setText(tr(&您已经输入&)+QString::number(textEdit-&toPlainText().length())+tr(&个字符(包括换行回车等)&));
this-&setWindowTitle(fileName+&(*)&+&-Notepad+&);
changed=1;
void MainWindow::pressOk_have()
QString content=textEdit-&toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
QTextStream save(&file);
this-&setWindowTitle(fileName+&-Notepad+&);
changed=0;
void MainWindow::pressOk_new()
fileName = QFileDialog::getSaveFileName(this, tr(&保存文件&),
&untitled.txt&,
tr(&文本文件 (*.txt)&));
QString content=textEdit-&toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
QTextStream save(&file);
this-&setWindowTitle(fileName+&-Notepad+&);
changed=0;
void MainWindow::newFile()
if(changed==1)
QMessageBox msg(this);
msg.setText(tr(&文件已改变&));
msg.setInformativeText(tr(&要将修改保存到&)+fileName+&?&);
msg.setIcon(QMessageBox::Question);
msg.setStandardButtons(QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
msg.setDefaultButton(QMessageBox::Save);
int ret = msg.exec();
switch(ret)
case QMessageBox::Save:
if(fileName!=tr(&无标题&))
pressOk_have();
textEdit-&clear();
fileName=tr(&无标题&);
this-&setWindowTitle(fileName+&-Notepad+&);
pressOk_new();
textEdit-&clear();
fileName=tr(&无标题&);
this-&setWindowTitle(fileName+&-Notepad+&);
case QMessageBox::No:
textEdit-&clear();
fileName=tr(&无标题&);
this-&setWindowTitle(fileName+&-Notepad+&);
case QMessageBox::Cancel:
textEdit-&clear();
fileName=tr(&无标题&);
this-&setWindowTitle(fileName+&-Notepad+&);
changed=0;
void MainWindow::saveAs()
pressOk_new();
void MainWindow::findText()
FindDialog findit(this);
connect(&findit,SIGNAL(findNext(QString,bool)),this,SLOT(findNowNext(QString,bool)));
connect(&findit,SIGNAL(findPrevious(QString,bool)),this,SLOT(findNowPre(QString,bool)));
findit.exec();
void MainWindow::findNowPre(QString str,bool b)
case false:
if(textEdit-&find(str,QTextDocument::FindBackward))
QMessageBox::warning(this,tr(&无法找到&),tr(&已到开头或结尾,找不到&)+str+&!&);
case true:
if(textEdit-&find(str,QTextDocument::FindBackward|QTextDocument::FindCaseSensitively))
QMessageBox::warning(this,tr(&无法找到&),tr(&已到开头或结尾,找不到&)+str+&!&);
void MainWindow::findNowNext(QString str,bool b)
case false:
if(textEdit-&find(str))
QMessageBox::warning(this,tr(&无法找到&),tr(&已到开头或结尾,找不到&)+str+&!&);
case true:
if(textEdit-&find(str,QTextDocument::FindCaseSensitively))
QMessageBox::warning(this,tr(&无法找到&),tr(&已到开头或结尾,找不到&)+str+&!&);
void MainWindow::setUndo(bool b)
if(b==true)
undoAt-&setEnabled(true);
undoAt-&setEnabled(false);
void MainWindow::repText()
ReplDialog rep(this);
connect(&rep,SIGNAL(sigRep(QString,QString,bool)),this,SLOT(repNow(QString,QString,bool)));
connect(&rep,SIGNAL(sigAll(QString,QString,bool)),this,SLOT(repAllNow(QString,QString,bool)));
rep.exec();
void MainWindow::repNow(QString s1, QString s2, bool b)
QTextCursor textCur=textEdit-&textCursor();
case false:
if(textEdit-&find(s1))
textCur=textEdit-&textCursor();
textCur.insertText(s2);
QMessageBox::warning(this,tr(&提示&),tr(&找不到需要替换的内容,可能光标在要查找的内容之后!&),QMessageBox::Ok);
case true:
if(textEdit-&find(s1,QTextDocument::FindCaseSensitively))
textCur=textEdit-&textCursor();
textCur.insertText(s2);
QMessageBox::warning(this,tr(&提示&),tr(&找不到需要替换的内容,可能光标在要查找的内容之后!&),QMessageBox::Ok);
void MainWindow::repAllNow(QString s1, QString s2, bool b)
qDebug()&&&Run&;
QTextCursor textSt;
QTextCursor textCur=textEdit-&textCursor();
textCur.movePosition(QTextCursor::Start);
textEdit-&setTextCursor(textCur);
case false:
while(textEdit-&find(s1))
textSt=textEdit-&textCursor();
textSt.insertText(s2);
case true:
while(textEdit-&find(s1,QTextDocument::FindCaseSensitively))
textSt=textEdit-&textCursor();
textSt.insertText(s2);
#include &finddialog.h&
#include &QtGui&
FindDialog::FindDialog(QWidget *parent):QDialog(parent)
setupWidget();
setConns();
void FindDialog::setupWidget()
findLabel=new QLabel(tr(&查找内容:&),this);
inText=new QLineEdit(this);
checkBox=new QCheckBox(tr(&大小写敏感&),this);
next=new QPushButton(tr(&查找下一个&),this);
cancel=new QPushButton(tr(&取消&),this);
up=new QRadioButton(tr(&向前&),this);
down=new QRadioButton(tr(&向后&),this);
down-&setDown(true);
QHBoxLayout *topLayout=new QHBoxL
QHBoxLayout *botLayout=new QHBoxL
QVBoxLayout *rightLayout=new QVBoxL
QVBoxLayout *leftLayout=new QVBoxL
QHBoxLayout *mainLayout=new QHBoxL
QHBoxLayout *cenLayout=new QHBoxL
QGroupBox *upOrDown=new QGroupBox(tr(&方向&),this);
cenLayout-&addWidget(up);
cenLayout-&addWidget(down);
upOrDown-&setLayout(cenLayout);
topLayout-&addWidget(findLabel);
topLayout-&addWidget(inText);
botLayout-&addWidget(checkBox);
botLayout-&addWidget(upOrDown);
rightLayout-&addWidget(next);
rightLayout-&addWidget(cancel);
leftLayout-&addLayout(topLayout);
leftLayout-&addLayout(botLayout);
mainLayout-&addLayout(leftLayout);
mainLayout-&addLayout(rightLayout);
this-&setLayout(mainLayout);
void FindDialog::setConns()
connect(cancel,SIGNAL(clicked()),this,SLOT(close()));
connect(next,SIGNAL(clicked()),this,SLOT(sendsgl()));
connect(up,SIGNAL(toggled(bool)),this,SLOT(setUp(bool)));
void FindDialog::sendsgl()
if(uod==NEXT)
emit findNext(this-&inText-&text(),checkBox-&isChecked());
qDebug()&&&send&&&checkBox-&isChecked()&&
emit findPrevious(this-&inText-&text(),checkBox-&isChecked());
qDebug()&&&send&&&checkBox-&isChecked()&&
void FindDialog::setUp(bool n)
#include &repldialog.h&
#include &QtGui&
ReplDialog::ReplDialog(QWidget *parent):QDialog(parent)
setupWidget();
setConns();
void ReplDialog::setupWidget()
scLabel=new QLabel(tr(&查找内容:&),this);
reLabel=new QLabel(tr(&替换为
:&),this);
scEdit=new QLineEdit(this);
reEdit=new QLineEdit(this);
repButton=new QPushButton(tr(&替换&),this);
repAllButton=new QPushButton(tr(&替换全部&),this);
cancelButton=new QPushButton(tr(&取消&),this);
sensCheckBox=new QCheckBox(tr(&大小写敏感&),this);
QHBoxLayout *topLayout=new QHBoxL
QHBoxLayout *midLayout=new QHBoxL
QVBoxLayout *leftLayout=new QVBoxL
QVBoxLayout *rightLayout=new QVBoxL
QHBoxLayout *mainLayout=new QHBoxL
topLayout-&addWidget(scLabel);
topLayout-&addWidget(scEdit);
midLayout-&addWidget(reLabel);
midLayout-&addWidget(reEdit);
leftLayout-&addLayout(topLayout);
leftLayout-&addLayout(midLayout);
leftLayout-&addWidget(sensCheckBox);
rightLayout-&addWidget(repButton);
rightLayout-&addWidget(repAllButton);
rightLayout-&addWidget(cancelButton);
mainLayout-&addLayout(leftLayout);
mainLayout-&addLayout(rightLayout);
this-&setLayout(mainLayout);
void ReplDialog::setConns()
connect(cancelButton,SIGNAL(clicked()),this,SLOT(close()));
connect(repButton,SIGNAL(clicked()),this,SLOT(sendRep()));
connect(repAllButton,SIGNAL(clicked()),this,SLOT(sendAll()));
void ReplDialog::sendRep()
emit sigRep(scEdit-&text(),reEdit-&text(),sensCheckBox-&isChecked());
qDebug()&&sensCheckBox-&isChecked();
void ReplDialog::sendAll()
emit sigAll(scEdit-&text(),reEdit-&text(),sensCheckBox-&isChecked());
用了QTextEdit
写的很烂 凑合着看看
主要写了打开 保存 另存为 查找 替换
其它都没写
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:45642次
排名:千里之外
原创:31篇
转载:15篇
评论:30条
(1)(2)(1)(3)(1)(1)(1)(4)(3)(1)(1)(2)(1)(1)(1)(4)(5)(3)(1)(1)(2)(2)(1)(2)(2)}

我要回帖

更多关于 qgroupbox 边框颜色 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信