Qt项目中的常见用法
来源:互联网 发布:java instance 编辑:程序博客网 时间:2024/06/11 05:16
- 1.QWidget设置为类似如右键菜单
- setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);
- 2.QTreeWidget创建parent item刷新问题
- ui->treeRoom->resizeColumnToContents(0);
- 3.
- setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);
- setAttribute(Qt::WA_NoSystemBackground, true);
- setAttribute(Qt::WA_TranslucentBackground, true);
- setAttribute(Qt::WA_DeleteOnClose);
- bool eventFilter(QObject *obj, QEvent *e){
- if(obj == this){
- if(QEvent::WindowDeactivate == e->type()){
- close();
- e->accept();
- return true;
- }
- }
- return QWidget::eventFilter(obj, e);
- }
- 4.QTextCursor insertImage相同图片只显示一个,解决:
- QTextImageFormat imageFormat;
- imageFormat.setName(imagePath);
- this->textCursor().insertImage(imageFormat, QTextFrameFormat::InFlow);
- 5.设置背景色
- QPalette palette = this->palette();
- palette.setBrush(QPalette::Background, QBrush(QColor(0, 123, 122)));
- this->setPalette(palette);
- this->setAutoFillBackground(true);
- paintEvent:
- QPainter painter(this);
- painter.setBrush(QColor("#1e1e21"));
- painter.setPen(Qt::NoPen);
- painter.drawRect(this->rect());
- 6.设置QTextEdit可输入个数:
- void MyWidget::slotTextChanged()
- {
- if (0 == m_count) {
- return;
- }
- QString curText = this->toPlainText();
- int len = curText.count();
- if (len > m_count) {
- int pos = this->textCursor().position();
- QTextCursor textCursor = this->textCursor();
- curText.remove(pos - (len - m_count), len - m_count);
- this->setText(curText);
- textCursor.setPosition(pos - (len - m_count));
- this->setTextCursor(textCursor);
- }
- }
- 7.QLineEdit设置搜索图标和清除按钮
- searchAction = new QAction(QIcon(qutil::skin("icon_search")), "", this);
- m_lineEdit->addAction(searchAction, QLineEdit::LeadingPosition);
- m_lineEdit->setClearButtonEnabled(true);
- QAction *clearAction = m_lineEdit->findChild<QAction*>(QLatin1String("_q_qlineeditclearaction"));
- if (clearAction) {
- clearAction->setIcon(QIcon(":/xyz");
- connect(clearAction, &QAction::triggered, [this] () {
- emit sigClear();
- });
- }
- 8.水平线
- QFrame *line = new QFrame(this);
- line->setMaximumSize(QSize(16777215, 1));
- line->setStyleSheet(QStringLiteral("background:rgb(68, 70, 73);"));
- line->setFrameShape(QFrame::HLine);
- line->setFrameShadow(QFrame::Sunken);
- 9.使用全局热键QxtGlobalShortcut
- if (!m_gsCaptureScreen->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_A))) {
- /////////////
- }
- 10.改变showNormal之后的大小setGeometry
- 11.无title窗口拉伸(windows环境)
- bool MyWindow::nativeEvent(const QByteArray & eventType, void * message, long * result)
- {
- Q_UNUSED(eventType);
- if (this->isMaximized()) {
- return false;
- }
- const int HIT_BORDER = 5;
- const MSG *msg=static_cast<MSG*>(message);
- if(msg->message == WM_NCHITTEST) {
- int xPos = ((int)(short)LOWORD(msg->lParam)) - this->frameGeometry().x();
- int yPos = ((int)(short)HIWORD(msg->lParam)) - this->frameGeometry().y();
- if(this->childAt(xPos,yPos) == 0) {
- *result = HTCAPTION;
- } else {
- return false;
- }
- if(xPos > 0 && xPos < HIT_BORDER) {
- *result = HTLEFT;
- }
- if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0)) {
- *result = HTRIGHT;
- }
- if(yPos > 0 && yPos < HIT_BORDER) {
- *result = HTTOP;
- }
- if(yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {
- *result = HTBOTTOM;
- }
- if(xPos > 0 && xPos < HIT_BORDER && yPos > 0 && yPos < HIT_BORDER) {
- *result = HTTOPLEFT;
- }
- if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > 0 && yPos < HIT_BORDER) {
- *result = HTTOPRIGHT;
- }
- if(xPos > 0 && xPos < HIT_BORDER && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {
- *result = HTBOTTOMLEFT;
- }
- if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {
- *result = HTBOTTOMRIGHT;
- }
- return true;
- }
- return false;
- }
- 12.安装全局事件过滤器
- class GlobalEventFilter : public QObject
- {
- Q_OBJECT
- public:
- GlobalEventFilter(QObject *parent);
- protected:
- bool eventFilter(QObject *, QEvent *);
- };
- GlobalEventFilter *globalEventFilter = new GlobalEventFilter(this);
- qApp->installEventFilter(globalEventFilter);
- 13.QTreeWidget遍历
- QTreeWidget *myTree;
- QTreeWidgetItemIterator iter(myTree);
- while (*iter) {
- ++iter;
- }
- 14.这里需要特别注意一点,如果QWidget直接show出来,是有背景色的,
- 但是如果它作为一个父QWidget的子窗口时就没有背景了!此时需要添加如下代码:
- this->setAutoFillBackground(true);
- QPalette palette = this->palette();
- palette.setColor(QPalette::Background, QColor("#36383D"));
- this->setPalette(palette);
- 15.模态对话框
- QEventLoop eventLoop;
- myWidget->setAttribute(Qt::WA_ShowModal, true);
- myWidget->show();
- connect(myWidget, &QObject::destroyed, &eventLoop, &QEventLoop::quit, Qt::DirectConnection);
- eventLoop.exec();
- 16.QTreeWidget删除item
- QTreeWidgetItemIterator iter(ui->treeRoom);
- while (*iter) {
- delete item;
- }
- 17.窗口抖动
- void Shake::start(QWidget *targetWidget, int number, int range)
- {
- Q_ASSERT(NULL != targetWidget);
- m_widget = targetWidget;
- m_number = number;
- m_range = range;
- m_start = 0;
- m_point = m_widget->pos();
- m_widget->raise();
- m_widget->activateWindow();
- bool isMaximized = m_widget->isMaximized();
- if (NULL == m_timer)
- {
- m_timer = new QTimer(this);
- QObject::connect(m_timer, &QTimer::timeout, [this, isMaximized] {
- if (m_start < m_number * 4)
- {
- ++m_start;
- switch (m_start % 4)
- {
- case 1:
- {
- QPoint newPos(m_point.x(), m_point.y() - m_range);
- m_widget->move(newPos);
- }
- break;
- case 2:
- {
- QPoint newPos(m_point.x() - m_range, m_point.y() - m_range);
- m_widget->move(newPos);
- }
- break;
- case 3:
- {
- QPoint newPos(m_point.x() - m_range, m_point.y());
- m_widget->move(newPos);
- }
- break;
- default:
- m_widget->move(m_point);
- break;
- }
- } else {
- m_timer->stop();
- }
- });
- }
- m_timer->start(40);
- }
18.如果刷新有问题可以调用:Widget->style()->polish(this);
19. QComboBox 对齐// First : Set the combobox the editable (this allows us to use the lineEdit)
mComboBox->setEditable(true);
// Second : Put the lineEdit in read-only mode
mComboBox->lineEdit()->setReadOnly(true);
// Third : Align the lineEdit to right
mComboBox->lineEdit()->setAlignment(Qt::AlignRight);
// Fourth : Align each item in the combo to the right
for(int i = 0; i < mComboBox->count(); i++)
mComboBox->setItemData(i, Qt::AlignRight, Qt::TextAlignmentRole);
20.// 去掉输入法输入时的虚线,但是会导致输入法输入框位置不正确
void TextEdit::inputMethodEvent(QInputMethodEvent *e)
{
if (e->commitString().isEmpty() && !e->preeditString().isEmpty()) {
return;
}
QTextEdit::inputMethodEvent(e);
}
0 0
- Qt项目中的常见用法
- Qt项目中的常见用法
- qt中的常见错误
- QT中的setAttribute()用法
- QT中的static用法
- 项目中的常见文件
- QT StyleSheet常见用法和样式
- Qt之QTableWidegt类常见用法
- STL中的map常见用法
- C++中的引用常见用法
- C++中的map常见用法
- Qt项目crash常见原因以及解决办法
- 实际项目中的常见算法
- 实际项目中的常见算法
- 实际项目中的常见算法
- 实际项目中的常见算法
- 实际项目中的常见算法
- 实际项目中的常见算法
- catalog start with 和 catalog backuppiece
- Android 内存优化- ANR 详解
- 欢迎使用CSDN-markdown编辑器
- C#ftp上传下载
- java 当前时间减去7天
- Qt项目中的常见用法
- Centos6.5/6.8 64位缺少防火墙配置文件及内部的相应配置
- LInux的启动过程
- excel导入导出
- 什么是java 序列化,如何实现java 序列化?
- 2016秋招笔试面试题一:Java及基础部分
- 161230工作笔记之设计模式
- 面试题 HashMap 数据结构 实现原理
- 如何在代码中设置textView跑马灯效果(不是在xml中)