Qt项目中的常见用法

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);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值